내 (상위) BlogPosts 테이블의 카운터 캐시가 (자식) 주석 테이블. 처음에는 내 earlier question에 제공된 답변이 해결책 일 수 있다고 생각했으나 어제 밤 잠자리에 들었을 때 일어난 일이 있습니다. 오늘 아침에 일어나서 레일즈 콘솔을 다시 시작하면 BlogPosts (실제로는 Post-id # 1)가 관련 자녀를 찾을 수 없습니다. 나는 Comments 테이블을 체크하고 내가 만든 5 개의 댓글이 모두 post_id = 1에 첨부되어 있습니다. 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.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는 콘솔에 댓글을 작성이 게시물에 추가 한 다음 3 별도의 단계로 표시 할 수 있습니다 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
예, 내 테스트 주석 5 개가 모두 표시되며 모두 1의 post_id를가집니다. 내 블로그 게시물의 ID는 1입니다. –