且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Rails中实现计数器缓存?

更新时间:2023-12-02 16:28:34

我用示例代码运行了Rails应用程序,对我来说一切正常.

I ran through a sample rails app with your code and it all worked fine for me.

我建议像hurikhan77所建议的那样进行调试,并查看dain建议的仅仅是@posts/@post问题.

I would suggest debugging a little more like hurikhan77 is suggesting and see if it is just the @posts / @post issue that dain suggested.

此外,请尝试在控制台中创建包含一些非常简单内容的帖子和评论,以查看其是否有效.

Also, try creating a post and a comment in the console with some very simple content to see if it is working.

$ ruby script/console

# add whatever fields are necessary to create     
> @p = Post.create(:title => "TestPost1")
  # => #<Post id: 3, ...

# again, add whatever is necessary to create
> @c = @p.comments.create(:comment => "TestComment1")
  # => #<Comment id: 8, ...

> Post.find(:last).comments_count
  # => 1

看看能帮到您什么.

/JP