Rails ActiveRecord 关联更新不一致

发布于 2024-08-21 13:06:26 字数 2256 浏览 8 评论 0原文

我遇到了一些我不理解的 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:在以下代码中,我创建一个带有关联 CommentPost,通过 创建第二个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴随着你 2024-08-28 13:06:26

这与 Active Record 缓存请求的方式以及处理 has_many 关联的方式有关。

除非在查找过程中使用 :include 选项预先加载关联。在需要之前,Rails 不会填充找到的记录的关联。当需要关联时,会进行一些记忆来减少执行的SQL查询数量。

单步执行问题中的代码:

post1 = Post.new(:title => 'Post 1')
comment1 = Comment.new(:content => 'content 1')
post1.comments << comment1  # updates post1's internal comments cache
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   # updates post1's internal comments cache

# Note that both Comments are associated with both Post objects even
# though I never explicitly associated it with post2.
post1.comment_ids # => [12, 13]

# this is the first time post2.comments are loaded. 
# SELECT comments.* FROM comments JOIN comments.post_id = posts.id WHERE posts.id = #{post2.id}
post2.comment_ids # => [12, 13]

场景 2:

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')

# first time post2.comments are loaded. 
# SELECT comments.* FROM comments JOIN comments.post_id = posts.id WHERE 
#   posts.id = post2.comments #=> Returns one comment (id = 14)
# cached internally.

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 has already been cached, so the SQL query is not executed again.

post2.comment_ids # => [14]

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:

post1 = Post.new(:title => 'Post 1')
comment1 = Comment.new(:content => 'content 1')
post1.comments << comment1  # updates post1's internal comments cache
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   # updates post1's internal comments cache

# Note that both Comments are associated with both Post objects even
# though I never explicitly associated it with post2.
post1.comment_ids # => [12, 13]

# this is the first time post2.comments are loaded. 
# SELECT comments.* FROM comments JOIN comments.post_id = posts.id WHERE posts.id = #{post2.id}
post2.comment_ids # => [12, 13]

Scenario 2:

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')

# first time post2.comments are loaded. 
# SELECT comments.* FROM comments JOIN comments.post_id = posts.id WHERE 
#   posts.id = post2.comments #=> Returns one comment (id = 14)
# cached internally.

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 has already been cached, so the SQL query is not executed again.

post2.comment_ids # => [14]

N.B. post2.comment_ids is internally defined as post2.comments.map(&:id)

P.S. My answer to this question might help you understand why post2 gets updated despite your not touching it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文