Table Relationships Problems

I have the following tables setup:

locations
:id
:name

systemmessages
:id
:name

systemmessage_validtimes
:id
:name
:systemmessage_id
:location_id
:start
:end

The systemmessage_validtimes has a ‘has_many’ relationship with
systemmessages, meaning that there could be many valid times for each
system message.
@systemMessage = Systemmessage.find(1)

E.g. I can access a system valid time like this :

@systemMessage.systemmessage_datetimeranges[0].id

I went ahead and made systemmessage_validtimes belong_to locations. But
when I try to access the locations variable, it doesn’t exist. The
location object isn’t being joined into the return object. I can
retireve
the location_id, but the following fails :

@systemMessage.systemmessage_datetimeranges[0].location.name

I did an inspect on the @systemMessage object, and ‘location’ object
does
not exist.

Is there a problem with nested relationships such as the one outlined
above?

Jim

Am Mittwoch, den 15.03.2006, 16:35 -0800 schrieb Jim J.:

systemmessage_validtimes
@systemMessage = Systemmessage.find(1)

E.g. I can access a system valid time like this :

@systemMessage.systemmessage_datetimeranges[0].id

I went ahead and made systemmessage_validtimes belong_to locations. But
when I try to access the locations variable, it doesn’t exist. The
location object isn’t being joined into the return object. I can retireve
the location_id, but the following fails :

Above you speak about belongs_to locations. I think you mean belongs_to
location. Singular.

@systemMessage.systemmessage_datetimeranges[0].location.name

I did an inspect on the @systemMessage object, and ‘location’ object does
not exist.

Is there a problem with nested relationships such as the one outlined above?

No, there is no problem and what you looks ok. Can you please show your
SystemmessageValidtimes model code to us.

–
Norman T.

http://blog.inlet-media.de

Thanks for the reply Norman.

I am stupid. I was doing a find(@id) on the record, and I should have
been calling my model function,

Systemmessage.findSystemMessage(@id)

Calling the model function seems to transcribe the heirarchy correctly.
So, you have to implement a find function in your model to transcribe
the full data set (a standard find() on the model won’t do this)?

Or am I misidentifying the issue?

Thanks again Norman.

Jim

Should be using belongs_to, not has_many from that side.

Am Donnerstag, den 16.03.2006, 19:01 +0100 schrieb Jim J.:

Or am I misidentifying the issue?

Thanks again Norman.

Jim

Sorry, but i can’t follow you. ActiveRecord supplies all basic methods
to navigate through your models.

What does Systemmessage.findSystemMessage(@id) return?

–
Norman T.

http://blog.inlet-media.de