Search code examples
ruby-on-railsruby-on-rails-4rolify

Get users which has specific role ONLY - Rolify


There is a dropdown which displays the list of sales reps which is defined as follows.

<%= f.select :sales_rep_id,
          User.with_role(:sales_rep).order(:email).map { |u| [u.email, u.id] },
          { include_blank: true },
          class: 'form-control' %>

The sales_rep users may also have other roles such as developer.

I need to hide the sales rep users who have the developer role as well.

Something like

User.where.not(has_role?(:developer)).with_role :sales_rep

Any idea on how to achieve this.


Solution

  • non_developers = User.enabled.order(:email).without_role(:developer)
    sales_reps = non_developers.with_role(:sales_rep)
    

    worked like a charm...