# Table Relationships Problems

**URL:** <https://www.ruby-forum.com/t/table-relationships-problems/55003>\
**Category:** Rails\
**Created:** [March 16, 2006, 1:38am UTC](https://www.ruby-forum.com/t/table-relationships-problems/55003 "2006-03-16T01:38:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aantix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@aantix](https://www.ruby-forum.com/u/aantix)\
**Post date:** [March 16, 2006, 1:38am UTC](https://www.ruby-forum.com/t/table-relationships-problems/55003/1 "2006-03-16T01:38:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aantix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@aantix](https://www.ruby-forum.com/u/aantix)\
**Post date:** [March 16, 2006, 11:24am UTC](https://www.ruby-forum.com/t/table-relationships-problems/55003/2 "2006-03-16T11:24:23Z")

</div>

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](http://blog.inlet-media.de)

---

<div class="post-metadata">

**Author:** ![aantix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@aantix](https://www.ruby-forum.com/u/aantix)\
**Post date:** [March 16, 2006, 7:01pm UTC](https://www.ruby-forum.com/t/table-relationships-problems/55003/3 "2006-03-16T19:01:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aantix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@aantix](https://www.ruby-forum.com/u/aantix)\
**Post date:** [March 17, 2006, 2:13am UTC](https://www.ruby-forum.com/t/table-relationships-problems/55003/4 "2006-03-17T02:13:42Z")

</div>

Should be using belongs\_to, not has\_many from that side.

---

<div class="post-metadata">

**Author:** ![aantix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@aantix](https://www.ruby-forum.com/u/aantix)\
**Post date:** [March 16, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/table-relationships-problems/55003/5 "2006-03-16T20:00:21Z")

</div>

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](http://blog.inlet-media.de)
