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.
Comments
Post a Comment