How to link two tables on a Rail page?


#1

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.


#2

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


#3

On 12/10/05, Cedric G. 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.


#4

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


#5

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-


#6

On 12/10/05, Cedric G. removed_email_address@domain.invalid 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.