开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜