Gagan
Gagan

Reputation: 573

Fetch record based on role rails

I am using Devise and Cancan for my projects and it works fine. There are users table in database. There are 3 types of role

->Admin
->Publishers
->Players

Now i want to fetch all record from users table expect admin's record.

I am little confused how to do this.

Upvotes: 0

Views: 90

Answers (1)

zolter
zolter

Reputation: 7160

If role is a string in user table:

scope :with_role, lambda{|role_name| where(:role => role_name) }

If user belongs_to role:

scope :with_role, lambda{|role_name| includes(:role).where(:roles => {:title => role_name}) }

And now you can fetch all admins:

User.with_role('admin')

Upvotes: 1

Related Questions