# Belongs\_to relation busted....why?

**URL:** <https://www.ruby-forum.com/t/belongs-to-relation-busted-why/55828>\
**Category:** Rails\
**Created:** [March 24, 2006, 3:08am UTC](https://www.ruby-forum.com/t/belongs-to-relation-busted-why/55828 "2006-03-24T03:08:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![meltman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@meltman](https://www.ruby-forum.com/u/meltman)\
**Post date:** [March 24, 2006, 3:08am UTC](https://www.ruby-forum.com/t/belongs-to-relation-busted-why/55828/1 "2006-03-24T03:08:40Z")

</div>

Ok, been banging my head on this and just can’t figure it. Normally, any  
table that has a belongs\_to to another table you can easily access it  
by:

table1.table2.field

And it works perfectly for one table I have. Let’s call it… tags. So,  
I’ve got:

Tag -\> has\_many :blah\_tags  
Blah -\> has\_many :blah\_tags  
Blah\_tag -\> belongs\_to :tag, belongs\_to :blah

Ok, works fine. Based on a blah\_tag entry, I can go blah\_tag.blah.name  
(or whatever). No problem.

Substitute, an identical setup for say Colour, and bam. The whole thing  
throws nils everywhere. I may have a big batch of valid colour\_tags, but  
try to go: colour\_tag.colour =\> NIL!

I don’t get it. Identical table setups…identical code… is colour  
reserved? Do I need to restart servers or something (tried that…)? Am  
I going crazy?

It makes no sense to me.

Thanks!  
mm

---

<div class="post-metadata">

**Author:** ![meltman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@meltman](https://www.ruby-forum.com/u/meltman)\
**Post date:** [March 24, 2006, 12:20pm UTC](https://www.ruby-forum.com/t/belongs-to-relation-busted-why/55828/2 "2006-03-24T12:20:17Z")

</div>

Am Freitag, den 24.03.2006, 03:08 +0100 schrieb meltman:

> Blah → has\_many :blah\_tags  
> reserved? Do I need to restart servers or something (tried that…)? Am  
> I going crazy?
> 
> It makes no sense to me.

Please show us some concrete code.

–  
Norman T.

[http://blog.inlet-media.de](http://blog.inlet-media.de)

---

<div class="post-metadata">

**Author:** ![meltman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@meltman](https://www.ruby-forum.com/u/meltman)\
**Post date:** [March 24, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/belongs-to-relation-busted-why/55828/3 "2006-03-24T14:16:42Z")

</div>

Well I don’t have it in front of me at the moment, but there’s nothing  
crazy going on:

class Colour \< ActiveRecord::Base  
has\_many :entry\_colours  
end

class Entry \< ActiveRecord::Base  
has\_many :entry\_colours  
end

class EntryColour \< ActiveRecord::Base  
belongs\_to :colour  
belongs\_to :entry  
end

class Search \< ApplicationController  
def search  
@colour = Colour.find(:first, :conditions =\> [“name=Red”])  
@results = EntryColour.find(:all,:conditions  
=\>[“colour\_id=?”,@colour.id])

#this bit is actually embedded in HTML but we’ll put it here…  
@results.each |result| do  
result.entry.name  
end  
end  
end

I don’t know if there’s just something out of whack… or what. I tried  
flushing the temp files, restarting everything… no dice. But another  
table with literally the identical setup works fine. Does it hate the  
word colour?

The things in @results are valid.

mm

Norman T. wrote:

> Am Freitag, den 24.03.2006, 03:08 +0100 schrieb meltman:
> 
> > Blah → has\_many :blah\_tags  
> > reserved? Do I need to restart servers or something (tried that…)? Am  
> > I going crazy?
> > 
> > It makes no sense to me.
> 
> Please show us some concrete code.
> 
> –  
> Norman T.
> 
> [http://blog.inlet-media.de](http://blog.inlet-media.de)

---

<div class="post-metadata">

**Author:** ![meltman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@meltman](https://www.ruby-forum.com/u/meltman)\
**Post date:** [March 24, 2006, 2:57pm UTC](https://www.ruby-forum.com/t/belongs-to-relation-busted-why/55828/4 "2006-03-24T14:57:01Z")

</div>

meltman wrote:

> Well I don’t have it in front of me at the moment, but there’s nothing  
> crazy going on:
> 
> class Colour \< ActiveRecord::Base  
> has\_many :entry\_colours  
> end
> 
> class Entry \< ActiveRecord::Base  
> has\_many :entry\_colours  
> end
> 
> class EntryColour \< ActiveRecord::Base  
> belongs\_to :colour  
> belongs\_to :entry  
> end

You seem to be querying a slightly odd way. If Colour has\_many  
EntryColours, you should be able to say @colour.entry\_colours.each{ |ec|  
…} Can you ?

Another thing to try is use script/console to load a Colour and inspect  
it.

Silly Question, perhaps, but is there a reason you’re specifically  
avoiding has\_and\_belongs\_to\_many ?

Then you could do  
@colour = Colour.find(:first, :conditions =\> [“name=Red”])  
@colour.entries.each{|entry| entry.name}  
or  
@entry.colours.each{|colour| colour.name}

Can you post the schema for those three tables ? It may be down to  
foreign key placement and/or whether the join table has it’s own id?

A.

---

<div class="post-metadata">

**Author:** ![meltman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@meltman](https://www.ruby-forum.com/u/meltman)\
**Post date:** [March 24, 2006, 3:19pm UTC](https://www.ruby-forum.com/t/belongs-to-relation-busted-why/55828/5 "2006-03-24T15:19:04Z")

</div>

Alan F. wrote:

> You seem to be querying a slightly odd way. If Colour has\_many  
> EntryColours, you should be able to say @colour.entry\_colours.each{ |ec|  
> …} Can you ?

Indeed I probably can (code not in front of me right now), or at least I  
should be. I’m relatively new to this whole Rails thing, and that  
certainly would be a much easier way to do the query.

> Silly Question, perhaps, but is there a reason you’re specifically  
> avoiding has\_and\_belongs\_to\_many ?

Hahaha, no, and I probably should be using it since the queries are  
certainly easier. Like I said, pretty new to the whole Rails thing, and  
really, to relational DBs in general. (I’m a recent perl convert whose  
been living in flat-file hell for too long!) So I appreciate the  
patience for my obviously moronic inquiries.

Ok, so the schema (also not in front of me is pretty basic):

table: colours  
id (int, auto-inc, primary key)  
name (varchar)

table: entries  
id (int, auto-inc, primary key)  
name (varchar)

table: entry\_colours  
colour\_id (int, primary key)  
entry\_id (int, primary key)

That’s it… it’s really just something I’m fooling around with to get  
my bearings in the new environment. What has me really mystified is that  
I had it working with another ‘join’ table or whatever, and then when I  
tried adding this second one, it didn’t work. So I’m a bit mystified.

Thanks again.  
mm

---

<div class="post-metadata">

**Author:** ![meltman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@meltman](https://www.ruby-forum.com/u/meltman)\
**Post date:** [March 24, 2006, 3:48pm UTC](https://www.ruby-forum.com/t/belongs-to-relation-busted-why/55828/6 "2006-03-24T15:48:20Z")

</div>

meltman wrote:

> Alan F. wrote:
> 
> > You seem to be querying a slightly odd way. If Colour has\_many  
> > EntryColours, you should be able to say @colour.entry\_colours.each{ |ec|  
> > …} Can you ?
> 
> Indeed I probably can (code not in front of me right now), or at least I  
> should be. I’m relatively new to this whole Rails thing, and that  
> certainly would be a much easier way to do the query.
> 
> > Silly Question, perhaps, but is there a reason you’re specifically  
> > avoiding has\_and\_belongs\_to\_many ?
> 
> Hahaha, no, and I probably should be using it since the queries are  
> certainly easier. Like I said, pretty new to the whole Rails thing, and

Aha! 🙂 Then life’s about to get easier.

So, here goes…

table: colours (id, name)  
table: entries (id, name)  
table: colours\_entries (colour\_id, entry\_id)

note its important the the two names in the join table be in alpha order  
for rails automagic to work…entries\_colours won’t work.

model: Colour, has\_and\_belongs\_to\_many :entries  
model: Entry, has\_and\_belongs\_to\_many :colours

You can now do:

red = Colour.find(1)  
red.entries.each{ |entry| entry.do\_something }

or  
entry = Entry.find(1}  
entry.colours.each{ |colour| colour.do\_something }

You can go from entry to color back to entry again…

entry.colours.each{ |colour| colour.entries.name }

For more info, see

> **[activerecord\_cheatsheet.pdf](https://www.slash7.com/cheats/activerecord_cheatsheet.pdf)**
>
> 51.59 KB

Alan

---

<div class="post-metadata">

**Author:** ![meltman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@meltman](https://www.ruby-forum.com/u/meltman)\
**Post date:** [March 24, 2006, 3:54pm UTC](https://www.ruby-forum.com/t/belongs-to-relation-busted-why/55828/7 "2006-03-24T15:54:44Z")

</div>

Alan F. wrote:

> Aha! 🙂 Then life’s about to get easier.

Indeed it has!

> note its important the the two names in the join table be in alpha order  
> for rails automagic to work…entries\_colours won’t work.

Oh my god! Alpha order! Man, is that documented somewhere?!

> model: Colour, has\_and\_belongs\_to\_many :entries  
> model: Entry, has\_and\_belongs\_to\_many :colours

Oh man, so much easier, so you don’t even _need_ the intermediate model  
anymore…

I swear, every day Rails gets better and better.

Thanks so much for the help!

mm

---

<div class="post-metadata">

**Author:** ![meltman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@meltman](https://www.ruby-forum.com/u/meltman)\
**Post date:** [March 24, 2006, 4:35pm UTC](https://www.ruby-forum.com/t/belongs-to-relation-busted-why/55828/8 "2006-03-24T16:35:34Z")

</div>

meltman wrote:

> Alan F. wrote:
> 
> > Aha! 🙂 Then life’s about to get easier.
> 
> Indeed it has!
> 
> > note its important the the two names in the join table be in alpha order  
> > for rails automagic to work…entries\_colours won’t work.
> 
> Oh my god! Alpha order! Man, is that documented somewhere?!

Indeed it is!  
[http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html](http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html)
