Forum: Ruby on Rails How to link two tables on a Rail page?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E421e675bba8dc31142a9873c0411907?d=identicon&s=25 Cedric Garcia (Guest)
on 2005-12-11 00:54
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 Garcia
4098ef08ed2ee3eabeb0ddf0ec03f783?d=identicon&s=25 railsonly (Guest)
on 2005-12-11 01:47
(Received via mailing list)
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
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 joevandyk (Guest)
on 2005-12-11 01:51
(Received via mailing list)
On 12/10/05, Cedric Garcia <ced_dude@hotmail.com> 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.
E421e675bba8dc31142a9873c0411907?d=identicon&s=25 Cedric Garcia <ced_dude@hotmail.com> (Guest)
on 2005-12-11 02:12
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
E421e675bba8dc31142a9873c0411907?d=identicon&s=25 Cedric Garcia <ced_dude@hotmail.com> <ced_dude@hot (Guest)
on 2005-12-11 02:23
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-
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 joevandyk (Guest)
on 2005-12-11 02:27
(Received via mailing list)
On 12/10/05, Cedric Garcia <ced_dude@hotmail.com> <ced_dude@hotmail.com>
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.
This topic is locked and can not be replied to.