Skip to content
Extends the functionality of ActiveRecord::Base#clone to perform a deep clone that includes user specified associations.
Pull request Compare This branch is 111 commits behind moiristo:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



This gem gives every ActiveRecord::Base object the possibility to do a deep clone. It is a rails3 upgrade of the deep_cloning plugin (


  • Activerecord, tested with Rails 3.

  • Rails 3.1 users, please check out the 'rails31' branch.


  • Install by adding the gem to your Gemfile

    gem 'deep_cloneable'

  • Rails 2:

    gem install deep_cloneable, '~> 1.2.4'

And include the gem in your apps config

config.gem 'deep_cloneable'
  • Or install as a plugin if you must:

    ./script/plugin install git://


Cloning one single association

pirate.clone :include => :mateys

Cloning multiple associations

pirate.clone :include => [:mateys, :treasures]

Cloning really deep

pirate.clone :include => {:treasures => :gold_pieces}

Cloning really deep with multiple associations

pirate.clone :include => [:mateys, {:treasures => :gold_pieces}]

Cloning really deep with multiple associations and a dictionary

A dictionary ensures that models are not cloned multiple times when it is associated to nested models. When using a dictionary, ensure recurring associations are cloned first:

pirate.clone :include => [:mateys, {:treasures => [:matey, :gold_pieces], :use_dictionary => true }]

If this is not an option for you, it is also possible to populate the dictionary manually in advance:

dict = { :mateys => {} }
pirate.mateys.each{|m| dict[:mateys][m] = m.clone }
pirate.clone :include => [:mateys, {:treasures => [:matey, :gold_pieces], :dictionary => dict }]

Cloning a model without an attribute

pirate.clone :except => :name

Cloning a model without multiple attributes

pirate.clone :except => [:name, :nick_name]

Cloning a model without an attribute or nested multiple attributes

pirate.clone :include => :parrot, :except => [:name, { :parrot => [:name] }]

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don't break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.


Copyright © 2011 Reinier de Lange. See LICENSE for details.

Something went wrong with that request. Please try again.