John
John

Reputation: 13715

In Ruby, how to make a new instance of a model with all the same attribute values (excepting id) of an existing instance

I am developing a Ruby on Rails app and I have a model called Activity. I want to be able to make a new instance of Activity and have it be an exact copy of an existing instance of Activity, except that it has a different ID. How can I do this?

Upvotes: 0

Views: 193

Answers (1)

Azolo
Azolo

Reputation: 4383

dup will make a "shallow copy", then save the new activity.

@new_activity = @old_activity.dup
@new_activity.save

Upvotes: 5

Related Questions