我一直试图弄清楚为什么我的(父) BlogPosts 表上的计数器缓存不会从(子) Comments 表中更新。起初我认为我之前的问题中提供的答案可能是解决方案,但昨晚我睡觉后发生了一些事情,因为当我今天早上醒来并重新启动我的 Rails 控制台时,我的 BlogPosts(实际上只有一个帖子 - id#1)无法找到他们关联的子评论。我检查了 Comments 表,我创建的五个评论都在那里,附加到 post_id = 1。前面问题中我的 Rails 控制台的输出表示帖子可以找到昨晚的评论。也许这解释了为什么计数器缓存没有更新,但我仍然不确定为什么父级无法找到它的子级。有什么提示吗?
Loading development environment (Rails 2.3.2)
>> p = Post.find(1)
p = Post.find(1)
=> #<Post id: 1, title: "test", content: "test", author_id: 1, status: "ok", created_at: "2009-05-21 19:27:14", updated_at: "2009-05-24 07:21:24", comments_count: 0>
>> p.comments.size
p.comments.size
=> 0
>> p.comments
p.comments
=> []
更新:这很奇怪——我再次重新启动了 Rails 控制台,但这次我在调用“p.comments.size”之前调用了 p.comments——它找到了评论!这里发生了什么?
Loading development environment (Rails 2.3.2)
>> p = Post.find 1
p = Post.find 1
=> #<Post id: 1, title: "test", content: "test", author_id: 1, status: "ok", created_at: "2009-05-21 19:27:14", updated_at: "2009-05-24 07:21:24", comments_count: 0>
>> p.comments
p.comments
=> [#<Comment id: 5, post_id: 1, author_id: 1, content: "Fifth Comment", status: "ok", created_at: "2009-05-24 07:08:56", updated_at: "2009-05-24 07:08:56">, #<Comment id: 4, post_id: 1, author_id: 1, content: "Fourth Comment", status: "ok", created_at: "2009-05-24 07:05:32", updated_at: "2009-05-24 07:05:32">, #<Comment id: 3, post_id: 1, author_id: 1, content: "Third Comment", status: "ok", created_at: "2009-05-24 06:34:59", updated_at: "2009-05-24 06:34:59">, #<Comment id: 2, post_id: 1, author_id: 1, content: "Second Comment", status: "ok", created_at: "2009-05-24 05:20:43", updated_at: "2009-05-24 05:20:43">, #<Comment id: 1, post_id: 1, author_id: 1, content: "First Comment", status: "ok", created_at: "2009-05-21 19:27:14", updated_at: "2009-05-21 19:27:14">]
>> p.comments.size
p.comments.size
=> 5
更新 2:按照 srboisvert 的建议,我创建了一个新评论并将其添加到帖子中。这有效,comments_counter 更新为 1:
Loading development environment (Rails 2.3.2)
>> p = Post.find 1
p = Post.find 1
=> #<Post id: 1, title: "test", content: "test", author_id: 1, status: "ok", created_at: "2009-05-21 19:27:14", updated_at: "2009-05-24 07:21:24", comments_count: 0>
>> com = Comment.new(:post_id => 1, :author_id => 1, :content => 'Sixth Comment', :status => 'ok')
com = Comment.new(:post_id => 1, :author_id => 1, :content => 'Sixth Comment', :status => 'ok')
=> #<Comment id: nil, post_id: 1, author_id: 1, content: "Sixth Comment", status: "ok", created_at: nil, updated_at: nil>
>> p.comments << com
p.comments << com
=> [#<Comment id: 6, post_id: 1, author_id: 1, content: "Sixth Comment", status: "ok", created_at: "2009-05-24 17:59:45", updated_at: "2009-05-24 17:59:45">, #<Comment id: 5, post_id: 1, author_id: 1, content: "Fifth Comment", status: "ok", created_at: "2009-05-24 07:08:56", updated_at: "2009-05-24 07:08:56">, #<Comment id: 4, post_id: 1, author_id: 1, content: "Fourth Comment", status: "ok", created_at: "2009-05-24 07:05:32", updated_at: "2009-05-24 07:05:32">, #<Comment id: 3, post_id: 1, author_id: 1, content: "Third Comment", status: "ok", created_at: "2009-05-24 06:34:59", updated_at: "2009-05-24 06:34:59">, #<Comment id: 2, post_id: 1, author_id: 1, content: "Second Comment", status: "ok", created_at: "2009-05-24 05:20:43", updated_at: "2009-05-24 05:20:43">, #<Comment id: 1, post_id: 1, author_id: 1, content: "First Comment", status: "ok", created_at: "2009-05-21 19:27:14", updated_at: "2009-05-21 19:27:14">]