I'm using Devise and Rolify.
Employee belongs_to User
and User has_one :employee
.
The User model has rolify
.
Because of Rolify, there is a table called user_roles
.
The following code works fine:
<% if current_user.has_role? :super %>
And this would work if I wanted to associate with the user instead of the employee:
:collection => User.with_role(:agent)
Now, I would like to have an association in a form that contains a select for employees that have a role of "agent".
The following tries don't work:
<%= f.association :employee, :collection => Employee.user.with_role(:agent), :label_method => :employee_full_name, :label => 'Agent' %>
The one above gives this error:
undefined method `user' for #<Class:0x007fac48215ee8>
This doesn't work either:
<%= f.association :employee, :collection => User.with_role(:agent).employee.id, :label_method => :employee_full_name, :label => 'Agent' %>
What would work?
Thanks for the help!
UPDATE1
The following works to display a list of employees with their associated role:
<td><%= employee.user.roles.first.name %></td>
UPDATE2
Because employee.user.roles.first.name
will give me the role name for an employee, I tried this:
Employee.where('employee.user.roles.first.name = ?', 'agent')
But, I get a PG error =
PG::Error: ERROR: improper qualified name (too many dotted names): employee.user.roles.first.name