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 w开发者_运维技巧ould 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.
You can put this code in an overriden Event.clone method
精彩评论