计数器缓存不更新,但我可以保存到父级和子级

发布于 2024-07-21 07:03:53 字数 6038 浏览 5 评论 0原文

我添加了计数器缓存,但无法更新它。 但我可以通过添加新博客文章来更新父级 - 博客帖子模型 - 并且我可以通过添加新评论来更新子级 - 评论模型。 计数器缓存应该通过自动更新 blog_posts.comments_count 字段来跟踪每个博客文章的评论总数。 我将概述我所经历的一些步骤,希望有人会注意到我做错了什么。 架构转储位于最后。

我有一个博客文章模型:

class Post < ActiveRecord::Base
  set_table_name("blog_posts")
  belongs_to :author, :class_name => "User", :foreign_key => 'author_id'
  has_many :comments, :class_name => "Comment",
    :foreign_key => 'post_id', :order => "created_at desc", :dependent => :destroy
  has_many :categorizations
  has_many :categories, :through => :categorizations
  named_scope :recent, :order => "created_at desc", :limit => 5

end

和一个将 counter_cache 设置为帖子模型的评论模型:

class Comment < ActiveRecord::Base
  belongs_to :post, :class_name => "Post", :foreign_key => "post_id", :counter_cache => true
  belongs_to :author, :class_name => "User", :foreign_key => "author_id"
end

我创建了一个迁移以将 counter_cache 列添加到 blog_posts 表中:

class AddCommentCounter < ActiveRecord::Migration
  def self.up
    add_column :blog_posts, :comments_count, :integer, :limit => 4, :default => 0, :null => false
    Post.find(:all).each do |post|
      current_count = post.comments.size
      post.update_attribute(:comments_count, current_count)
    end
  end

  def self.down
    remove_column :blog_posts, :comments_count
  end
end

但是迁移无法使用 current_count 更新博客文章。 它始终为零。

我打开 Rails 控制台尝试手动 update_attribute:

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:02:35", 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

>> p.comments_count
p.comments_count

=> 0

>> p.update_attribute(:comments_count, 5)
p.update_attribute(:comments_count, 5)

=> true

>> p.comments_count
p.comments_count

=> 5

>> p.save
p.save

=> true

但是当我查看数据库 comments_count = 0 时。

任何想法都会非常感激。

我的 schema.db 看起来像这样:

ActiveRecord::Schema.define(:version => 20090524055907) do

  create_table "blog_posts", :force => true do |t|
    t.string   "title",          :limit => 100, :default => "", :null => false
    t.text     "content",                                       :null => false
    t.integer  "author_id",                     :default => 0,  :null => false
    t.string   "status",         :limit => 20,  :default => "", :null => false
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "comments_count",                :default => 0,  :null => false
  end

  add_index "blog_posts", ["author_id"], :name => "index_blog_posts_on_author_id"

  create_table "categories", :force => true do |t|
    t.string   "name",        :limit => 50, :default => "", :null => false
    t.string   "short_name",  :limit => 30, :default => "", :null => false
    t.string   "description",               :default => "", :null => false
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "categories_posts", :force => true do |t|
    t.integer "category_id", :null => false
    t.integer "post_id",     :null => false
  end

  add_index "categories_posts", ["category_id"], :name => "index_categories_posts_on_category_id"
  add_index "categories_posts", ["post_id"], :name => "index_categories_posts_on_post_id"

  create_table "comments", :force => true do |t|
    t.integer  "post_id",                  :default => 0,  :null => false
    t.integer  "author_id",                :default => 0,  :null => false
    t.text     "content",                                  :null => false
    t.string   "status",     :limit => 25, :default => "", :null => false
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "sessions", :force => true do |t|
    t.string   "session_id", :default => "", :null => false
    t.text     "data"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id"
  add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at"

  create_table "users", :force => true do |t|
    t.string   "username",        :limit => 25, :default => "", :null => false
    t.string   "hashed_password", :limit => 40, :default => "", :null => false
    t.string   "first_name",      :limit => 25, :default => "", :null => false
    t.string   "last_name",       :limit => 40, :default => "", :null => false
    t.string   "email",           :limit => 50, :default => "", :null => false
    t.string   "display_name",    :limit => 25, :default => "", :null => false
    t.integer  "user_level",      :limit => 3,  :default => 0,  :null => false
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

I added a counter cache but can't get it to update. But I can update the parent - the Blog Post model by adding a new blog post - and I can update the child - Comments model - by adding a new comment. The counter cache is supposed to keep track of the total number of comments per blog post by auto-updating the blog_posts.comments_count field. I'll outline some of the steps I went through and hopefully someone will notice something I did wrong. The schema dump is at the end.

I have a Blog Post model:

class Post < ActiveRecord::Base
  set_table_name("blog_posts")
  belongs_to :author, :class_name => "User", :foreign_key => 'author_id'
  has_many :comments, :class_name => "Comment",
    :foreign_key => 'post_id', :order => "created_at desc", :dependent => :destroy
  has_many :categorizations
  has_many :categories, :through => :categorizations
  named_scope :recent, :order => "created_at desc", :limit => 5

end

and a Comments model with a counter_cache set to the post model:

class Comment < ActiveRecord::Base
  belongs_to :post, :class_name => "Post", :foreign_key => "post_id", :counter_cache => true
  belongs_to :author, :class_name => "User", :foreign_key => "author_id"
end

I created a migration to add the counter_cache column to the blog_posts table:

class AddCommentCounter < ActiveRecord::Migration
  def self.up
    add_column :blog_posts, :comments_count, :integer, :limit => 4, :default => 0, :null => false
    Post.find(:all).each do |post|
      current_count = post.comments.size
      post.update_attribute(:comments_count, current_count)
    end
  end

  def self.down
    remove_column :blog_posts, :comments_count
  end
end

But the migration fails to update the blog posts with the current_count. It's always zero.

I opened up the Rails console to try to update_attribute manually:

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:02:35", 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

>> p.comments_count
p.comments_count

=> 0

>> p.update_attribute(:comments_count, 5)
p.update_attribute(:comments_count, 5)

=> true

>> p.comments_count
p.comments_count

=> 5

>> p.save
p.save

=> true

But when I look in the database comments_count = 0.

Any ideas would be more than joyously appreciated.

My schema.db looks like this:

ActiveRecord::Schema.define(:version => 20090524055907) do

  create_table "blog_posts", :force => true do |t|
    t.string   "title",          :limit => 100, :default => "", :null => false
    t.text     "content",                                       :null => false
    t.integer  "author_id",                     :default => 0,  :null => false
    t.string   "status",         :limit => 20,  :default => "", :null => false
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "comments_count",                :default => 0,  :null => false
  end

  add_index "blog_posts", ["author_id"], :name => "index_blog_posts_on_author_id"

  create_table "categories", :force => true do |t|
    t.string   "name",        :limit => 50, :default => "", :null => false
    t.string   "short_name",  :limit => 30, :default => "", :null => false
    t.string   "description",               :default => "", :null => false
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "categories_posts", :force => true do |t|
    t.integer "category_id", :null => false
    t.integer "post_id",     :null => false
  end

  add_index "categories_posts", ["category_id"], :name => "index_categories_posts_on_category_id"
  add_index "categories_posts", ["post_id"], :name => "index_categories_posts_on_post_id"

  create_table "comments", :force => true do |t|
    t.integer  "post_id",                  :default => 0,  :null => false
    t.integer  "author_id",                :default => 0,  :null => false
    t.text     "content",                                  :null => false
    t.string   "status",     :limit => 25, :default => "", :null => false
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "sessions", :force => true do |t|
    t.string   "session_id", :default => "", :null => false
    t.text     "data"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id"
  add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at"

  create_table "users", :force => true do |t|
    t.string   "username",        :limit => 25, :default => "", :null => false
    t.string   "hashed_password", :limit => 40, :default => "", :null => false
    t.string   "first_name",      :limit => 25, :default => "", :null => false
    t.string   "last_name",       :limit => 40, :default => "", :null => false
    t.string   "email",           :limit => 50, :default => "", :null => false
    t.string   "display_name",    :limit => 25, :default => "", :null => false
    t.integer  "user_level",      :limit => 3,  :default => 0,  :null => false
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

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

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

发布评论

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

评论(1

知你几分 2024-07-28 07:03:53

查看 counter_cache 上 railscasts Episode 23 中更新的代码。

计数器属性为 attr_readonly。 也许您需要使用 update-counters 来代替更新属性在你的迁移中?

Take a look at the updated code from railscasts episode 23 on counter_cache.

The counter attributes are attr_readonly. Perhaps update-counters is what you need to use in place of update attributes in your migration?

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