Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't add a counter_cache #577

Open
miguelpeniche opened this issue Feb 11, 2022 · 0 comments
Open

Can't add a counter_cache #577

miguelpeniche opened this issue Feb 11, 2022 · 0 comments

Comments

@miguelpeniche
Copy link

We want to reduce database queries in our app, some of our users have more than one role so it would be nice to use user.roles.size > 1 without having the need of touching the database at all. I tried adding a roles_count field to users and adding a counter_cache like this without success:

class UserRole < ApplicationRecord
  acts_as_paranoid

  self.table_name = "users_roles"

  belongs_to :user, counter_cache: :roles_count
  belongs_to :role
end

I have tried adding counter_cache: :roles_count to every single association in Role and UserRole but I always get:

undefined method 'counter_cache_column' for nil:NilClass

Anybody have a suggestion?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant