ruby-on-rails - CounterCache 不适用于多态关联

标签 ruby-on-rails ruby ruby-on-rails-5 associations

我正在将 counter_cache 概念应用到我的论坛应用程序中。对于具有简单 belongs_to 关联但不适用于多态关联的模型来说,它工作得很好。

我的应用程序结构是这样的。我有 3 个模型,论坛、帖子和评论。

class Forum < ApplicationRecord
   has_many :posts
end

发布模型:

class Post < ApplicationRecord
  belongs_to :forum, counter_cache: true
  has_many :comments, as: :parent
end

评论模型:

class Comment < ApplicationRecord
  belongs_to :parent,polymorphic: true, counter_cache: true
  has_many :comments, as: :parent
end

我的评论模型基本上是一个多态模型,因此一条评论可以属于一个帖子,也可以属于另一个评论(这样它将被视为对一条评论的回复)

我在 Forum 模型中有一个 posts_count 字段,该字段工作正常,并且自动递增和递减功能正常工作。

我在 Post 模型中还有一个 comments_count 字段。 每当创建新评论时,相关帖子中的 comments_count 字段就会递增。 但是当我尝试创建一个评论,其父级(多态关联)是另一个评论(所以基本上是评论的回复)时,我遇到了一个错误:

Started POST "/comments" for 103.255.4.86 at 2018-10-18 20:48:39 +0000
Cannot render console from 103.255.4.86! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by CommentsController#create as JS
  Parameters: {"utf8"=>"✓", "comment"=>{"body"=>"testing a reply", "parent_id"=>"812", "parent_type"=>"Comment"}}
  Post Load (0.8ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT $2  [["id", 7], ["LIMIT", 1]]
   (0.4ms)  BEGIN
  Comment Load (1.6ms)  SELECT  "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT $2  [["id", 812], ["LIMIT", 1]]
  SQL (0.9ms)  INSERT INTO "comments" ("body", "parent_type", "parent_id", "owner_type", "owner_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["body", "testing a reply"], ["parent_type", "Comment"], ["parent_id", 812], ["owner_type", "User"], ["owner_id", 46], ["created_at", "2018-10-18 20:48:39.141170"], ["updated_at", "2018-10-18 20:48:39.141170"]]
   (0.4ms)  ROLLBACK
Completed 500  in 43ms (ActiveRecord: 7.2ms)



ActiveModel::MissingAttributeError (can't write unknown attribute `comments_count`):

我在这里缺少什么?任何提示将非常感激,谢谢!!

最佳答案

我通过从 Comment 模型中删除 counter_cache: true 并在 Comment 中定义自己的计数器递增和递减方法来使其正常工作> 模型。这是我的最终 Comment 模型:

class Comment < ApplicationRecord
  belongs_to :parent,polymorphic: true,touch: true
  has_many :comments,dependent: :destroy,as: :parent

  after_create_commit { self.parent_post.increment!(:answers_count,1) }
  after_destroy { self.parent_post.decrement!(:answers_count,1) }

  def parent_post
    (self.parent if parent_type == "Post") || self.parent.parent
  end

end

如果有人提出其他答案,请在此处发布。谢谢。

关于ruby-on-rails - CounterCache 不适用于多态关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52882437/

相关文章:

ruby-on-rails - 无法使用包含两个冒号的 css 属性在 nokogiri 中选择节点

ruby-on-rails - Clear rails Better Errors 控制台

ruby-on-rails - `method_missing':用户的未定义方法`devise'(调用'User.connection'建立连接)

ruby-on-rails - 使用 Rails 在同一页面上显示多个表单

ruby-on-rails - Rails javascript_include_tag 和 stylesheet_link_tag 上的错误 URI

ruby-on-rails - 将 Bootstrap collapse 设置为根据条件默认打开元素

ruby-on-rails - 如何在事件管理菜单中添加超赞字体图标

ruby-on-rails - 获取所有没有 child 的记录,即使它们存在于Elasticsearch中

javascript - 切换按钮操作 OnClick - JQuery + Rails

ruby-on-rails - Paperclip:与 mailboxer gem 集成