Project

deep_copy

0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
A method to deep copy AR objects, including their associations
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

Deep copy

Deep copy allows you to clone Active Record objects including its associations.

Installation

Just require the "deep_copy" gem on your project. On Rails 2.3, that is: config.gem "deep_copy" on your environment.rb

Usage

Pick any Active Record object and call deep_copy on it. You can also include associations and except fields, like this: new_user = user.deep_copy :include => :car, :except => [:name, {:car => [:number_of_wheels, :number_of_accidents]}] In this example we're cloning a user, including its car. However, we're not cloning the user's name nor are we cloning his car's number_of_wheels and number_of_accidents.

You can also go deeper (bad pun intended). In this example, we'll clone a lot of people (excluding their names): new_son = son.deep_copy :include => [{:parent => grand_parent}, :siblings], :except => [:name, {:parent => [:name, :grand_parent => :name]}, {:siblings => :name}]

Acknowledgements

This gem was inspired in Jan De Poorter's deep_cloning plugin. All of this work is his, except for the gem part and the field exceptions in associations.

Copyright (c) 2010 Gonçalo Silva