I have a users model and a companies model. Many users to many companies. This works great for normal users. It’s not so great for users with admin abilities. Admins need to see all companies. I’d rather not have to assign all companies to the admins.
Currently, I’m using methods inside my model like so:
class Company
def self.for_user
if User.current.is_admin?
Company.all
else
User.companies
end
end
end
Is there a way to make the has_and_belongs_to_many relationship contingent on a condition?
thanks to this question/answer, I figured it out
Modify the behavior of has_many or use scope?
so, my association becomes
Then you can call it with