简体   繁体   English

如何在Rails的多态has_one关联中禁用计数器缓存?

[英]How to disable counter cache in polymorphic has_one association on Rails?

I'm needing to disable counter cache for has_one associations as below and keep it just on has_many: 我需要为has_one关联禁用计数器缓存,如下所示,并将其保留在has_many上:

class User < ActiveRecord::Base
  has_one :email, as: :emailable
end

class Company < ActiveRecord::Base
  has_many :emails, as: :emailable
end

class Email < ActiveRecord::Base
  belongs_to :emailable, polymorphic: true, counter_cache: emails_count
end

I don't have a emails_count column on User model. 我在用户模型上没有emails_count列。 Any ideas? 有任何想法吗?

Thanks for your help! 谢谢你的帮助!

如果未指定counter_cache选项,则不应有任何计数器缓存,如果由于某种原因无论您是否进行缓存,都可以尝试强制使用counter_cache: false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM