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
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 aroles_count
field to users and adding acounter_cache
like this without success:I have tried adding
counter_cache: :roles_count
to every single association inRole
andUserRole
but I always get:undefined method 'counter_cache_column' for nil:NilClass
Anybody have a suggestion?