Forum: Ruby on Rails Table Relationships Problems.

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.
Jim J. (Guest)
on 2006-03-16 02:38
(Received via mailing list)
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
Norman T. (Guest)
on 2006-03-16 12:24
(Received via mailing list)
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
Jim J. (Guest)
on 2006-03-16 20:01
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
Norman T. (Guest)
on 2006-03-16 21:00
(Received via mailing list)
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
Julian L. (Guest)
on 2006-03-17 03:13
(Received via mailing list)
Should be using belongs_to, not has_many from that side.
This topic is locked and can not be replied to.