我一直在试图弄清楚为什么我的(父)BlogPosts表上的计数器缓存不会从(孩子)评论表。起初我认为我的earlier question中提供的答案可能是解决方案,但昨天晚上睡觉后发生了一些事情,因为当我今天早上醒来并重新启动Rails控制台时,我的BlogPosts(实际上只是一个Post-id#1)没有无法找到他们相关的孩子评论。我检查了评论表,并且我创建的五条评论都附在post_id = 1上。我的Rails控制台在earlier question中的输出表明该帖子可以在昨晚找到评论。也许这就解释了为什么计数器缓存没有更新,但我仍然不确定父母为什么不能找到它的子女。任何提示?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
=> []
UPDATE:这很奇怪 - 我再次但是这个时候,我叫p.comments之前我所谓的“p.comments.size”重新启动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
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">]
当您运行时,是否有任何对您的帖子签名的评论:Comment.all? – klew 2009-05-24 16:55:13
是的,我的所有五个测试评论都显示出来,所有文章的post_id都是1.我的博客帖子也有相应的ID为1. – 2009-05-24 16:59:54