1

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">]
Community
  • 1
  • 1
pez_dispenser
  • 4,394
  • 7
  • 37
  • 47

1 Answers1

1

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.

srboisvert
  • 12,679
  • 15
  • 63
  • 87
  • Good idea. I just tried this and it worked fine. The counter cache also got updated to 1. I'll put the Rails console output in my answer just in case. This is going in the right direction but seems to suggest taht the has_many and belongs_to are not actually messed up. And it still doesn't explain why the migration failed to update the pre-existing Post to report the correct number of pre-existing Comments. Nor why calling p.comments.size BEFORE p.comments in the Console would have a different effect. Thanks for the feedback. I'll continue tinkering. – pez_dispenser May 24 '09 at 18:09