has_one belongs_to confusion

Hello all, This will be my 3rd month with Ruby on Rails. I do all my developing on Mac with TextMate. I'm now totally confused. I have a one to one relationship. Projects and study_type - project model - belongs_to :study_type

- study_type model - has_one :project

I can display correct data in my "show" method but not on the "index"

- project_controller - def index   # SearchLogic   @projects = Project.search_all_projects(params[:search], params[:page]) end

def show   @project = Project.find(params[:id]) end

- index.html.erb - # part of table <% @projects.each do |project| %>   <tr class="<%= cycle('oddRow','evenRow') %>">   <td><%=h project.project_number %></td>     <td><%=h truncate( project.title, :length => 30 ) %></td>     <td><%=h truncate( project.description, :length => 30 ) %></td>     <td><%=h project.pi_full_name %></td>     <td><%=h project.project_department %></td>     <td><%=h project.study_type.name %></td>   <td class='irb'>     <% project.irbs.each do |f| %>     <%= f.IRB %><br />     <% end %>   </td>     <td class="project"><%= link_to 'View', project %></td>     <td class="action"><%= link_to 'Edit', edit_project_path(project) %></td>     <td class="action"><%= link_to 'Destroy', project, :confirm => "Are you sure?", :method => :delete %></td>   </tr> <% end %>

- show.html.erb - # part of table <tr>   <td class="label"><b>Study Type:</b></td>   <td><%=h @project.study_type.name %></td> </tr>

I'm not sure what I'm doing wrong.

Thanks for any advice or help.

JohnM

I can display correct data in my "show" method but not on the "index"

- project_controller - def index   # SearchLogic   @projects = Project.search_all_projects(params[:search], params[:page]) end

def index @projects = Project.search_all_projects(params[:search], params[:page], :include => :study_type) end

Without looking over all of the code here, you more than likely are missing an :include. I've added it above.

Thanks for the reply.

Without looking over all of the code here, you more than likely are missing an :include. I've added it above.

Sorry, the only missing code would be in the project model.

- project model -

def self.search_all_projects(search, page) # searchlogic gem used # project_number, title, and pi_full_name are column names that are searchable # via search bar on index page.    project_number_or_title_or_pi_full_name_like(search).descend_by_id.paginate(:page => page, :per_page => 20)   end

I tried using as you said with a standard find and it didn't help. I still get the "undefined method `name' for nil:NilClass" error.

John

John Mcleod wrote:

I tried using as you said with a standard find and it didn't help. I still get the "undefined method `name' for nil:NilClass" error.

John

That's a very common error and can happen in a number of different situations. Are you using both searchlogic and will_paginate? Make sure that any of your foreign keys, when called are being referenced by key_id instead of key.id. That error has happened to me in those particular situations. Otherwise, drill down into the error.

Troubleshoot your code in rails console and use reload! to apply some quick fixes or when testing to quickly reload your environment.

This could mean that one of your projects does not have a study_type, assuming it is the line containing <%= h @project.study_type.name %> that is failing. You could replace that code with <%=h @project.study_type.name if @project.study_type %> and see whether one of the projects shows nothing for this cell.

Alternatively (probably also in fact) have a look a the Rails Guide on debugging for how to debug a rails app.

Colin

<%=h @project.study_type.name if @project.study_type %> and see

   And from 2.3 it can be done like

@project.try(:study_type).try(:name)

Sijo

I *think* the direct equivalent is @project.study_type.try(:name) I am not sure whether I like this or not.

Colin