-
Notifications
You must be signed in to change notification settings - Fork 401
Open
Description
While we have a support for :any resource, I find it useful to query for users, who have 'any' role assigned on a specific resource or class.
Here's how it can be easily done sing plain ActiveRecord:
User.joins(:roles).where(roles: { resource_type: Project }).to_a
SELECT "users".* FROM "users" INNER JOIN "users_roles" ON "users_roles"."user_id" = "users"."id" INNER JOIN "roles" ON "roles"."id" = "users_roles"."role_id" WHERE "roles"."resource_type" = 'Project'
=> []
# and of course, same for a resource instance
User.joins(:roles).where(roles: { resource: Project.last }).to_sql
SELECT "users".* FROM "users" INNER JOIN "users_roles" ON "users_roles"."user_id" = "users"."id" INNER JOIN "roles" ON "roles"."id" = "users_roles"."role_id" WHERE "roles"."resource_type" = 'Project' AND "roles"."resource_id" = 'e0b45f5c-ace9-4e85-9f2e-9a132c2bed7c'"I can prepare a PR if there's a will to addit to the gem's API
User.with_role(:any, Project)Metadata
Metadata
Assignees
Labels
No labels