I think there is a slight inconsistency in Rails regarding adding associations between existing records. Assume Post has_many :tags. We have an analogy when we want to immediately add the association to either new or existing child: > Post.first.tags.create name: "my tag" # commits immediately > # and to existing tag > Post.first.tags << Tag.first # commits immediately But we don't have an analogy to build: > p = Post.first > p.tags.build name: "my tag" > p.save # commits all changes > # and to existing tag > # ? I think this is unfortunate, because I like the idea of building all changes in memory first and then commiting everything together. I have a project where this would be useful for me, but is currently not possible. Why not? Discuss.
on 2012-11-02 22:59
on 2012-11-03 16:12
For that, they use: p.reload and/or p.tags.reload The tag is saved when you save p (provided you have :autosave => true set) That's shown here: http://api.rubyonrails.org/classes/ActiveRecord/Au... under the examples.
on 2012-11-05 16:18
RTFS, and use backdoors :) Post.first.tags.target << Tag.first