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 immediatelyBut 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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/2H7Bo9y1t10J. For more options, visit https://groups.google.com/groups/opt_out.
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/AutosaveAssociation.html under the examples. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
RTFS, and use backdoors :) Post.first.tags.target << Tag.first On 02.11.2012, at 19:36, Jan Brdo <mrbrdo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/2H7Bo9y1t10J. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.