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.