RolifyCommunity / rolify

Role management library with resource scoping
https://rolifycommunity.github.io/rolify/
MIT License
3.16k stars 404 forks source link

Can't add a counter_cache #577

Open miguelpeniche opened 2 years ago

miguelpeniche commented 2 years ago

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?