Updated: Your mentioned method can be achieved as follows:
def self.find_users_with_role(role) role.users end
It is easy and can be done in 2 steps:
First, find the role from which you want to return all users.
Secondly, find all the users associated with this role.
all_users = role.users
It is assumed that you have correctly configured the relationship between the user and role models with the has_and_belongs_to_many relationship
class User < ActiveRecord::Base has_and_belongs_to_many :roles
and
class Role < ApplicationRecord has_and_belongs_to_many :users end
source share