You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Short version of question: How to make scopes on the User object so I can get a collection of Users who have access to a feature, while keeping all the policy stuff in the same place?
More information:
My application has many user classes, only some of whom can be assigned to tickets:
However, the user does not dispatch himself; an administrator does the dispatches. I would like a collection of User objects who can be dispatched to populate dropdown lists, for example. Currently I have the following, which of course works, but makes way more SQL queries than is necessary:
The solution that I don't want is to define a scope on the User model (User.can_dispatch.ordered) because then I'd have to change in two places if I wanted different classes of users to be able to have access to be dispatched.
All of the example scopes in the documentation are about returning records that one particular user has access to. In my case, I would like to return User records that have access to a feature. What is the commonly-accepted pattern for this?
Thank you in advance for any suggestions!
The text was updated successfully, but these errors were encountered:
Unfortunately this is not possible in the general case: the challenge is that methods in policies can contain arbitrary logic involving both the user and the record, and there's no general way to convert arbitrary logic into an ActiveRecord scope which does the same thing in the database.
I feel like you're not alone in raising this question, but we don't recommend any particular approach. See issue #368 for a related discussion.
Short version of question: How to make scopes on the User object so I can get a collection of Users who have access to a feature, while keeping all the policy stuff in the same place?
More information:
My application has many user classes, only some of whom can be assigned to tickets:
This works as expected:
However, the user does not dispatch himself; an administrator does the dispatches. I would like a collection of User objects who can be dispatched to populate dropdown lists, for example. Currently I have the following, which of course works, but makes way more SQL queries than is necessary:
= f.association :user, :collection => User.ordered.select { |u| FeaturesPolicy.new(u, :features).dispatch? }, :label_method => :full_name
The solution that I don't want is to define a scope on the User model (
User.can_dispatch.ordered
) because then I'd have to change in two places if I wanted different classes of users to be able to have access to be dispatched.All of the example scopes in the documentation are about returning records that one particular user has access to. In my case, I would like to return User records that have access to a feature. What is the commonly-accepted pattern for this?
Thank you in advance for any suggestions!
The text was updated successfully, but these errors were encountered: