# Has\_one belongs\_to confusion

**URL:** <https://www.ruby-forum.com/t/has-one-belongs-to-confusion/181292>\
**Category:** Rails\
**Created:** [January 8, 2010, 9:53pm UTC](https://www.ruby-forum.com/t/has-one-belongs-to-confusion/181292 "2010-01-08T21:53:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 8, 2010, 9:53pm UTC](https://www.ruby-forum.com/t/has-one-belongs-to-confusion/181292/1 "2010-01-08T21:53:48Z")

</div>

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| %\>

 \<%=h project.project\_number %\> \<%=h truncate( project.title, :length =\> 30 ) %\> \<%=h truncate( project.description, :length =\> 30 ) %\> \<%=h project.pi\_full\_name %\> \<%=h project.project\_department %\> \<%=h project.study\_type.name %\> \<% project.irbs.each do |f| %\> \<%= f.IRB %\>  
 \<% end %\> \<%= link\_to 'View', project %\> \<%= link\_to 'Edit', edit\_project\_path(project) %\> \<%= link\_to 'Destroy', project, :confirm =\> "Are you sure?", :method =\> :delete %\> \<% end %\>
- show.html.erb -

# part of table
**Study Type:** \<%=h @project.study\_type.name %\>

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

Thanks for any advice or help.

JohnM

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 8, 2010, 10:08pm UTC](https://www.ruby-forum.com/t/has-one-belongs-to-confusion/181292/2 "2010-01-08T22:08:11Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 8, 2010, 10:41pm UTC](https://www.ruby-forum.com/t/has-one-belongs-to-confusion/181292/3 "2010-01-08T22:41:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 9, 2010, 11:08am UTC](https://www.ruby-forum.com/t/has-one-belongs-to-confusion/181292/4 "2010-01-09T11:08:11Z")

</div>

2010/1/8 John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> …  
> 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.

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 9, 2010, 12:55am UTC](https://www.ruby-forum.com/t/has-one-belongs-to-confusion/181292/5 "2010-01-09T00:55:07Z")

</div>

John M. 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.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 10, 2010, 11:07am UTC](https://www.ruby-forum.com/t/has-one-belongs-to-confusion/181292/6 "2010-01-10T11:07:47Z")

</div>

2010/1/10 Sijo k g [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > \<%=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)

I _think_ the direct equivalent is  
@project.study\_type.try(:name)  
I am not sure whether I like this or not.

Colin

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 10, 2010, 7:42am UTC](https://www.ruby-forum.com/t/has-one-belongs-to-confusion/181292/7 "2010-01-10T07:42:58Z")

</div>

> \<%=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
