我遇到了一些我不理解的 Rails 2.3.5 ActiveRecord 行为。似乎一个对象可以以不一致的方式更新其关联 ID。
最好用一个例子来解释:
创建一个Post
带有字符串属性的模型'title'
和一个Comment
带有字符串属性的模型'content'
。
以下是关联:
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
场景#1:在下面的代码中,我创建Post
了一个关联的,通过'ing 第一个Comment
创建第二个,将第二个添加到第一个并发现第二个与第二个关联而没有显式分配。Post
find
Comment
Post
Post
Comment
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]
场景#2:再次运行上面的命令,但是这次插入一个额外的命令,从表面上看,应该不会影响结果。额外的命令post2.comments
是在创建之后comment2
和添加到.comment2
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]
请注意,在此场景中只有一个与相关联的评论,post2
而在场景 1 中则有两个。
大问题:为什么要post2.comments
在添加新的之前运行Comment
以post1
对与评论相关联的评论产生任何影响post2
?