Skip to content

Add support for :any role in with_role query #603

@januszm

Description

@januszm

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions