Skip to content

Instantly share code, notes, and snippets.

@be9
Created May 22, 2009 05:25
Show Gist options
  • Save be9/115944 to your computer and use it in GitHub Desktop.
Save be9/115944 to your computer and use it in GitHub Desktop.
# If you know of a better way to do this, please fork the gist!
def query_by_role(object, role_name)
corresponding_role_ids = object.accepted_roles.all(:conditions => { :name => role_name }).map(&:id)
# this will now return all users that have +role_name+ on +object+
User.all(:include => :roles, :conditions => ['roles.id IN (?)', corresponding_role_ids])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment