CounterCache无法用于多态关联

编程入门 行业动态 更新时间:2024-10-11 17:30:02
本文介绍了CounterCache无法用于多态关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在我的论坛应用程序中实现counter_cache概念.对于具有简单belongs_to关联但不适用于多态关联的模型,它工作正常.

I am implementing counter_cache concept to my Forums app. It is working fine for one model that has a simple belongs_to association but not working for a polymorphic association.

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

My app structure is like this. I have 3 models, Forum, Post and Comment.

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

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

My Comments Model is basically a polymorphic one, so a comment can belong to a post or a comment can belong to another comment (in this way it will be considered a reply of a comment)

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

I have a posts_count field in Forum model which is working fine and auto-incrementing and decrementing is working.

我在Post模型中也有一个comments_count字段. 每当创建新注释时,相关帖子中的comments_count字段都会增加. 但是,当我尝试创建其父级(多态关联)是另一个评论(因此基本上是评论的回复)的评论时,我遇到了一个错误:

I also have a comments_count field in Post model. Whenever a new comment is created, the comments_count field is incremented in the associated Post. But when I try to create a comment whose parent (polymorphic association) is another comment (so basically a reply of a comment), I hit an error:

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`):

我在这里想念什么?任何提示将不胜感激,谢谢!!!

What am I missing here? Any hint would be really appreciated, Thanks!!

推荐答案

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

I have got it working by removing counter_cache: true from Comment model and defining my own methods for increment and decrement of the counter in Comment model. So here is my final Comment model:

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

如果有人想出另一个答案,请在此处发布.谢谢.

If anyone comes up with another answer please post it here. Thanks.

更多推荐

CounterCache无法用于多态关联

本文发布于:2023-08-03 14:25:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287664.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多态   CounterCache

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!