# Trouble linking models

**URL:** <https://www.ruby-forum.com/t/trouble-linking-models/167681>\
**Category:** Rails\
**Created:** [May 11, 2009, 1:55pm UTC](https://www.ruby-forum.com/t/trouble-linking-models/167681 "2009-05-11T13:55:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [May 11, 2009, 1:55pm UTC](https://www.ruby-forum.com/t/trouble-linking-models/167681/1 "2009-05-11T13:55:30Z")

</div>

Class User \< ActiveRecord::Base  
has\_many :articles

class Article \< ActiveRecord::Base  
belongs\_to :user

After a user posts an article, I want to include their username. I  
presume I should just be able to use \<%= article.user.screen\_name %\> in  
the articles/index.  
This give me:

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.screen\_name

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [May 11, 2009, 2:28pm UTC](https://www.ruby-forum.com/t/trouble-linking-models/167681/2 "2009-05-11T14:28:40Z")

</div>

Sean,  
did you check your database? Is user\_id filled up correctly?

tom

Sean S. wrote:

> You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.screen\_name

# –

Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

# [www.meinlschmidt.com](http://www.meinlschmidt.com) www.maxwellrender.cz www.lightgems.cz

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [May 12, 2009, 5:17pm UTC](https://www.ruby-forum.com/t/trouble-linking-models/167681/3 "2009-05-12T17:17:23Z")

</div>

I know this doesn’t help the core problem, but it might help in  
debugging to set that column to ‘NOT NULL’ in your database, and set a  
default value.

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [May 17, 2009, 1:17pm UTC](https://www.ruby-forum.com/t/trouble-linking-models/167681/4 "2009-05-17T13:17:51Z")

</div>

I can get \<%= article.user\_id %\> to print the id number of the user.  
But when I try to use \<%= article.user.screen\_name %\> I get the error:

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.screen\_name

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [May 17, 2009, 2:50pm UTC](https://www.ruby-forum.com/t/trouble-linking-models/167681/5 "2009-05-17T14:50:45Z")

</div>

Could this mean that the user\_id field in the article table is filled in  
but  
there is no user with that id? Have a look in the database. I would  
have  
thought this might produce a different error though.

If the db is ok use the ruby debugger to break at that point and see  
what  
the variables are set to.

Colin

2009/5/17 Sean S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
