I have a table which list a set amount of managers. In the Manager User Id column I am trying to get the name to appear instead of the integer. I have the following relationships
User model
has_many :roles, :through => :roles_users
has_many :projects, :foreign_key => "manager_user_id"
Projects
class Project < ActiveRecord::Base
belongs_to :user, :foreign_key => "manager_user_id"
Index.html.erb
<tr>
<td><%= project.project_number %></td>
<td><%= project.project_name %></td>
**<td><%= user.project.manager_user_id%></td>**
<td><center><%= link_to image_tag("icons/user.png"), project %></center></td>
<td><center><%= link_to image_tag("icons/user_edit.png"), edit_project_path(project) %></center></td>
<td><center><%= link_to image_tag("icons/user_delete.png"), project, :confirm => 'Are you sure?', :method => :delete %></center></td>
</tr>
I have tried the following:
- project.user.manager_user_id
- user.project.manager_user_id
Neither of these work and I get this error: undefined local variable or method `user’ for #<#:0xb14cf5c>
What is it that I am doing wrong. I used the above similar method for another table and this worked fine.
It looks from your view that you have
projectdefined in the view, but notuser. That being so, you definitely want theproject.userform. That also makes sense in that a Project only has one associated User, whereas a User has multiple Projects, souser.projectis an array.Secondly, remember that
manager_user_idis a member ofProject, not ofUser, soproject.user.manager_user_idwon’t work.I suspect you want something like
project.user.name.