Copy model instances in Rails
a wrong way to do this would be:
f2 = Foo.new( f1.attributes ) # wrong!
f2.save # wrong!
or in one line, but still wrong:
f2 = Foo.create( f1.attributes ) # wrong!
see comments for details
This is what ActiveRecord::Base#clone method is for:
@bar = @foo.clone
@bar.save
You can make duplicate record in rails like
@bar = @foo.dup
@bar.save!
As per the following question, if you are using Rails >= 3.1, you can use object.dup
:
What is the easiest way to duplicate an activerecord record?