pixelearth
pixelearth

Reputation: 14630

How do I clone/copy a model instance and preserve its associations?

Right now I have the following, which works fine:

  e                    = Event.find(params[:based_on_id])
  @event               = e.clone
  @event.tag_ids       = e.tag_ids
  @event.subcategories = e.subcategories

However I would like to avoid the last two lines, especially since my model may change its association structure in the future. I just want a pristine "new" copy, which includes all associations.

Note: the associations above are has_many :through.

Upvotes: 0

Views: 274

Answers (1)

Sohan
Sohan

Reputation: 3805

You can put this code in an overriden Event.clone method

Upvotes: 1

Related Questions