I have a tough time figuring out when associations are saved. I can of 
course refer to the associations guide, or test from the console each time 
(which is what I do), but is there a general underlying thumb rule that I 
can use?

For example: 

class Blog < ActiveRecord::Base
 has_many :posts
end

class Post < ActiveRecord::Base
 belongs_to :blog
end

blog = Blog.create(name:'My Travel Blog')
post = Post.create(name: 'My first post')
blog.posts << post
# is post's blog_id field already updated in the database? or do I need to 
call post.save? Will blog.save work as well?

Perhaps there is an underlying principle, such as (*this is only an 
example, it is not generally true*) - associations are never saved without 
explicit call to save on the model that has a foreign key field. 


-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/50433fa8-cb96-45f7-98bc-4c21baec6c5d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to