Rails ActiveRecord 关联更新不一致
我遇到了一些我不理解的 Rails 2.3.5 ActiveRecord 行为。对象的关联 ID 似乎可以以不一致的方式更新。
通过一个示例可以最好地解释这一点:
使用字符串属性 'title'
创建一个 Post
模型,并使用字符串属性 创建一个
。Comment
模型>'内容'
以下是关联:
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
场景 #1:在以下代码中,我创建一个带有关联 Comment
的 Post
,通过 创建第二个
'第一个,将第二个Post
>找到Comment
添加到第一个Post
并发现第二个Post
有第二个< code>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 中则有两条评论。
大问题:为什么在将新的 Comment
添加到 post1
之前运行 post2.comments
会对与 关联的评论产生任何影响发布2?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这与 Active Record 缓存请求的方式以及处理 has_many 关联的方式有关。
除非在查找过程中使用 :include 选项预先加载关联。在需要之前,Rails 不会填充找到的记录的关联。当需要关联时,会进行一些记忆来减少执行的SQL查询数量。
单步执行问题中的代码:
场景 2:
NB
post2.comment_ids
内部定义为post2.comments.map(&:id)
PS 我对 这个问题可能有帮助你明白为什么 post2 尽管你没有碰过它却被更新了。
This has to do with the way that Active Record caches requests and the way that has_many associations are handled.
Unless the association is eagerloaded with an :include option during the find. Rails will not populate the association for the found records until needed. When the association is needed some memoization is done to cut down on the number of SQL queries executed.
Stepping through the code in the question:
Scenario 2:
N.B.
post2.comment_ids
is internally defined aspost2.comments.map(&:id)
P.S. My answer to this question might help you understand why post2 gets updated despite your not touching it.