Rails 控制台重新启动后,父模型无法找到关联的子模型对象(然后可以)

发布于 2024-07-20 11:22:17 字数 4010 浏览 4 评论 0原文

我一直试图弄清楚为什么我的(父)BlogPosts 表上的计数器缓存不会从(子)Comments 表中更新。 起初我认为我的 之前的问题可能是解决方案,但昨晚我上床睡觉后发生了一些事情,因为当我今天早上醒来并重新启动我的Rails控制台时,我的BlogPosts(实际上只是一篇帖子 - id#1)无法找到其关联的子评论。 我检查了评论表,我创建的五个评论都在那里,附加到 post_id = 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>

>> 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">]

I've been stuck trying to figure out why a counter cache on my (parent) BlogPosts table won't update from the (child) Comments table. At first I thought the answer provided in my earlier question might be the solution but something happened after I went to bed last night because when I woke up this morning and restarted my Rails console, my BlogPosts (actually just one Post - id# 1) aren't able to find their associated child Comments. I checked the Comments table and the five comments I create are all there, attached to post_id = 1. The output from my Rails console in the earlier question indicates that the post could find the comments last night. Perhaps this explains why the counter cache was not updating but I'm still not sure why the parent would not be able to find its children. Any hints?

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: This is strange - I restarted the Rails console again but this time I called p.comments BEFORE I called "p.comments.size" - AND IT FOUND THE COMMENTS!! What's going on here?

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

UPDATE 2: Following srboisvert's advice I created a new Comment and added it to the Post. This worked and the comments_counter updated to 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">]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

缺⑴份安定 2024-07-27 11:22:18

您可以在控制台中创建评论,将其添加到帖子中,然后分 3 个单独的步骤显示它吗?

你正在做很多非默认的fk名称规范(尽管你的名字似乎与rails期望的没有那么不同,所以你可能只想使用约定)所以我的猜测是,不知何故你的 has_many属_to 被搞乱了。

Can you create a comment in the console, add it to a post and then display it in 3 separate steps?

You are doing lots of non-default fk name specification (though your names don't seem to be that different from what rails would expect so you might want to just use the conventions)so my guess is that somehow your has_many belongs_to are messed up.

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