0

I'm needing to disable counter cache for has_one associations as below and keep it just on 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. Any ideas?

Thanks for your help!

leompeters
  • 886
  • 1
  • 13
  • 24

1 Answers1

0

If you don't specify a counter_cache option, there should not be any counter caching, if caching happens regardless for you for some reason, you could try forcing a counter_cache: false

Samer Buna
  • 8,821
  • 9
  • 38
  • 55
  • Hi Samer, in this case, I want caching only Company class and not User class, do you have any idea? – leompeters Sep 29 '14 at 22:38
  • I don't think there is a clean way to do this, you could always monkeypatch your way to that though, I think if you override increment/decrement_counter methods on the User class you might get away with what you need here. If I were you though, I wouldn't use counter_cache at all, and would manually update the counters for Company whenever you add/delete emails. – Samer Buna Sep 30 '14 at 21:28