I am running into some Rails 2.3.5 ActiveRecord behavior I do not understand. It appears that an object can have its association ids updated in inconsistent ways.
This is best explained with an example:
Create a Post
model with the string attribute 'title'
and a Comment
model with the string attribute 'content'
.
Here are the associations:
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
Scenario #1: In the following code I create one Post
with an associated Comment
, create a second Post
by find
'ing the first, add a second Comment
to the first Post
and discover that the second Post
has the second Comment
associated to it without an explicit assignment.
post1 = Post.new
post1 = Post.new(:title => 'Post 1')
comment1 = Comment.new(:content => 'content 1')
post1.comments << comment1
post1.save
# Create a second Post object by find'ing the first
post2 = Post.find_by_title('Post 1')
# Add a new Comment to the first Post object
comment2 = Comment.new(:content => 'content 2')
post1.comments << comment2
# Note that both Comments are associated with both Post objects even
# though I never explicitly associated it with post2.
post1.comment_ids # => [12, 13]
post2.comment_ids # => [12, 13]
Scenario #2: Run the above commands again but this time insert one extra command that, on the face of it, should not affect the results. The extra command is post2.comments
which occurs after creating comment2
and before adding comment2
to post1
.
post1 = Post.new
post1 = Post.new(:title => 'Post 1A')
comment1 = Comment.new(:content => 'content 1A')
post1.comments << comment1
post1.save
# Create a second Post object by find'ing the first
post2 = Post.find_by_title('Post 1A')
# Add a new Comment to the first Post object
comment2 = Comment.new(:content => 'content 2A')
post2.comments # !! THIS IS THE EXTRA COMMAND !!
post1.comments << comment2
# Note that both Comments are associated with both Post objects even
# though I never explicitly associated it with post2.
post1.comment_ids # => [14, 15]
post2.comment_ids # => [14]
Note that there is only one comment associated with post2
in this scenario whereas in Scenario 1 there were two.
The Big Question: Why would running post2.comments
before adding the new Comment
to post1
make any difference to which Comments were associated with post2
?