ruby on rails - What is the easiest way to duplicate an activerecord record? -


i want make copy of activerecord record, changing single field in process (in addition id). simplest way accomplish this?

i realize create new record, , iterate on each of fields copying data field-by-field - figured there must easier way this...

such as:

 @newrecord=record.copy(:id)  *perhaps?* 

to copy, use clone (or dup rails 3.1) method:

# rails < 3.1 new_record = old_record.clone  #rails >= 3.1 new_record = old_record.dup 

then can change whichever fields want.

activerecord overrides built-in object#clone give new (not saved db) record unassigned id.
note not copy associations, you'll have manually if need to.

rails 3.1 clone shallow copy, use dup instead...


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -