# How to link two tables on a Rail page?

**URL:** <https://www.ruby-forum.com/t/how-to-link-two-tables-on-a-rail-page/49957>\
**Category:** Rails\
**Created:** [December 11, 2005, 12:54am UTC](https://www.ruby-forum.com/t/how-to-link-two-tables-on-a-rail-page/49957 "2005-12-11T00:54:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cedric\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/65b543/32.png) [@Cedric\_G](https://www.ruby-forum.com/u/Cedric_G)\
**Post date:** [December 11, 2005, 12:54am UTC](https://www.ruby-forum.com/t/how-to-link-two-tables-on-a-rail-page/49957/1 "2005-12-11T00:54:11Z")

</div>

I have 2 MYSQL tables correctly linked to my Rail application.

Table#1 represents the class Users  
Table#2 represents the class Timezone

I can display the fields I want on Rails pages such as user/view or  
timezone/view (when only accessing one table in a page).

However I am not able to view a page that would include a few columns of  
BOTH the user table + one column from the timezone table. Both tables  
are linked by the id field.

I believe the model is properly defined in \model by using a “belong to”  
and “has many” statements to link both table.

- I try to display the timezone column by using  
user.timezone.time\_zone\_name but I systematically get the following  
error message (winXP, rail .14, msql 5, webrick):

" You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.time\_zone\_name"

Any idea what is the problem?

Thanks,  
Cedric G.

---

<div class="post-metadata">

**Author:** ![Cedric\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/65b543/32.png) [@Cedric\_G](https://www.ruby-forum.com/u/Cedric_G)\
**Post date:** [December 11, 2005, 1:47am UTC](https://www.ruby-forum.com/t/how-to-link-two-tables-on-a-rail-page/49957/2 "2005-12-11T01:47:11Z")

</div>

Hi Cedric,  
Apparently the user you are referencing does not have a timezone. In  
the  
timezone table is there a timezone entry whose user\_id is the same as  
the ID  
of the user you are referencing in the view?  
-Frank

---

<div class="post-metadata">

**Author:** ![Cedric\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/65b543/32.png) [@Cedric\_G](https://www.ruby-forum.com/u/Cedric_G)\
**Post date:** [December 11, 2005, 1:51am UTC](https://www.ruby-forum.com/t/how-to-link-two-tables-on-a-rail-page/49957/3 "2005-12-11T01:51:15Z")

</div>

On 12/10/05, Cedric G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> BOTH the user table + one column from the timezone table. Both tables  
> The error occured while evaluating nil.time\_zone\_name"
> 
> Any idea what is the problem?

user.timezone is nil.

---

<div class="post-metadata">

**Author:** ![Cedric\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/65b543/32.png) [@Cedric\_G](https://www.ruby-forum.com/u/Cedric_G)\
**Post date:** [December 11, 2005, 2:12am UTC](https://www.ruby-forum.com/t/how-to-link-two-tables-on-a-rail-page/49957/4 "2005-12-11T02:12:17Z")

</div>

railsonly wrote:

> Hi Cedric,  
> Apparently the user you are referencing does not have a timezone. In  
> the  
> timezone table is there a timezone entry whose user\_id is the same as  
> the ID  
> of the user you are referencing in the view?  
> -Frank

In my timezone table I have:

- 2 columns: {id} and {time\_zone\_name}
- 3 rows: {1} {Pacific}, {2} {Mountain}, {3} {Central}.

In my user table I have

- a few columns that include {id}, {user\_id}, …, (user\_time\_zone\_id}
- so the rows are: {1} {1} {username1} … {2}, {2} {2} {username2} …  
{3} and {3} {3} {username3} … {1}.

It is the user\_time\_zone\_id that establishes the link between the two  
tables so that instead of displaying “3” in the timezone for user2 I  
should have “Central” displayed.

Suprisingly, I made typo and used user.timezone.id in the list.rhtml and  
I got another error message like this one: “Called id for nil, which  
would mistakenly be 4 – if you really wanted the id of nil, use  
object\_id”

–\> it is very surprising to me that is “4” is mentionned. Because no id  
is = to 4. No idea where this is coming from.

Thanks.  
Cedric

---

<div class="post-metadata">

**Author:** ![Cedric\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/65b543/32.png) [@Cedric\_G](https://www.ruby-forum.com/u/Cedric_G)\
**Post date:** [December 11, 2005, 2:23am UTC](https://www.ruby-forum.com/t/how-to-link-two-tables-on-a-rail-page/49957/5 "2005-12-11T02:23:04Z")

</div>

When going thru the Rails doc I came accros this one:

"… when you want to create a one-to-many foreign key relationship  
between, say, authors (note the plural!) and stories, you have to use a  
column author\_id in stories. The foreign key must be named in the format  
of: _singularOfForeignTableName_\_id. "

I did not do that! I changed the field to timezone\_id in my user table  
and everything worked perfectly. Thanks for the help

C-

---

<div class="post-metadata">

**Author:** ![Cedric\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/65b543/32.png) [@Cedric\_G](https://www.ruby-forum.com/u/Cedric_G)\
**Post date:** [December 11, 2005, 2:27am UTC](https://www.ruby-forum.com/t/how-to-link-two-tables-on-a-rail-page/49957/6 "2005-12-11T02:27:54Z")

</div>

On 12/10/05, Cedric G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> - 2 columns: {id} and {time\_zone\_name}
> 
> Suprisingly, I made typo and used user.timezone.id in the list.rhtml and  
> I got another error message like this one: “Called id for nil, which  
> would mistakenly be 4 – if you really wanted the id of nil, use  
> object\_id”
> 
> → it is very surprising to me that is “4” is mentionned. Because no id  
> is = to 4. No idea where this is coming from.

The id of ‘nil’ is 4. user.timezone is nil, and you’re asking for the  
id of nil.

user.timezone needs to be set to a Timezone object. Or, you need to  
check to see if user.timezone is nil, and if it is, don’t try to  
display any information about it.
