# Relationships: one model referencing same table for differen

**URL:** <https://www.ruby-forum.com/t/relationships-one-model-referencing-same-table-for-differen/98353>\
**Category:** Rails\
**Created:** [July 14, 2006, 1:42am UTC](https://www.ruby-forum.com/t/relationships-one-model-referencing-same-table-for-differen/98353 "2006-07-14T01:42:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ali\_Williams](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58f4c7/32.png) [@Ali\_Williams](https://www.ruby-forum.com/u/Ali_Williams)\
**Post date:** [July 14, 2006, 1:42am UTC](https://www.ruby-forum.com/t/relationships-one-model-referencing-same-table-for-differen/98353/1 "2006-07-14T01:42:01Z")

</div>

Hi,

After I’ve saved, I want to display the Description from the Codes table  
that  
corresponds with the ID in the Cause table.

Rails doesn’t seem to like that, because on my form, I have two sets of  
data in  
combo boxes that come from the Codes table. So, in the show template, I  
can’t  
do this:

Cause of Death: \<%= @cause.code.description %\>

Any ideas?

The database I’m converting from had all the code tables broken out, but  
I  
wanted to combine them all into one code table. There must be some way  
to  
display the text instead of the id!!

Any and all help greatly appreciated!

Ali

---

<div class="post-metadata">

**Author:** ![Ali\_Williams](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58f4c7/32.png) [@Ali\_Williams](https://www.ruby-forum.com/u/Ali_Williams)\
**Post date:** [July 14, 2006, 1:55am UTC](https://www.ruby-forum.com/t/relationships-one-model-referencing-same-table-for-differen/98353/2 "2006-07-14T01:55:17Z")

</div>

Lots of ideas, but I’d need to see your migrations / table layouts and  
your  
models… there’s not enough here to go on.

---

<div class="post-metadata">

**Author:** ![Ali\_Williams](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58f4c7/32.png) [@Ali\_Williams](https://www.ruby-forum.com/u/Ali_Williams)\
**Post date:** [July 14, 2006, 9:43pm UTC](https://www.ruby-forum.com/t/relationships-one-model-referencing-same-table-for-differen/98353/3 "2006-07-14T21:43:20Z")

</div>

Does this help? Please let me know - I am pulling my hair out over  
this. Thank  
you!!

class Cause \< ActiveRecord::Base  
has\_many :codes  
end

class Code \< ActiveRecord::Base  
belongs\_to :codetype  
belongs\_to :cause  
end

## – – Table structure for table `causes`

CREATE TABLE `causes` (  
`id` int(11) NOT NULL auto\_increment,  
`case_id` int(11) NOT NULL default ‘0’,  
`cod_id` int(11) NOT NULL default ‘0’,(code.codetype\_id where  
codetype\_id = 4)  
`mod_id` int(11) default ‘0’,(code.codetype\_id where codetype\_id = 8)  
`cause_texta` varchar(255) default NULL,  
`cause_textb` varchar(120) default NULL,  
`cause_textc` varchar(120) default NULL,  
`cause_textd` varchar(150) default NULL,  
`date_entered` datetime NOT NULL default ‘0000-00-00 00:00:00’,  
`entered_by` varchar(15) NOT NULL default ‘’,  
PRIMARY KEY (`id`),  
KEY `cod_id` (`cod_id`),  
KEY `case_id` (`case_id`)  
) TYPE=MyISAM AUTO\_INCREMENT=31 ;

## – – Table structure for table `codes`

CREATE TABLE `codes` (  
`id` int(11) NOT NULL auto\_increment,  
`codetype_id` int(11) NOT NULL default ‘0’,  
`abbreviation` varchar(20) default NULL,  
`description` varchar(50) default NULL,  
`other` varchar(35) default NULL,  
`obsolete_date` date default NULL,  
PRIMARY KEY (`id`),  
KEY `codetype_id` (`codetype_id`)  
) TYPE=MyISAM AUTO\_INCREMENT=317 ;

---

<div class="post-metadata">

**Author:** ![Ali\_Williams](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58f4c7/32.png) [@Ali\_Williams](https://www.ruby-forum.com/u/Ali_Williams)\
**Post date:** [July 14, 2006, 10:28pm UTC](https://www.ruby-forum.com/t/relationships-one-model-referencing-same-table-for-differen/98353/4 "2006-07-14T22:28:22Z")

</div>

Well… it’s not gonna work because your tables aren’t following the  
conventions.

You have no relationship that Rails can understand in your table  
How can I explain this… What you have here is just not going to work.

If you want a cause to have many codes, then the foreign key needs to be  
in  
the other table… the codes table needs to have the cause\_id column.

If you want to have the code\_id in the cause table, it needs to be  
called  
code\_id

Then you’d be able to do

class Cause \< ActiveRecord::Base  
belongs\_to :code  
end

Does a cause have multiple codes? If it does, then you need to create a  
causes\_codes table and use has\_and\_belongs\_to\_many

# causes\_codes

cause\_id  
code\_id

class Cause \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :codes  
end

class Code \< ActiveRecord::Base  
belongs\_to :codetype  
has\_and\_belongs\_to\_many :causes  
end

Need more help? let me know.

---

<div class="post-metadata">

**Author:** ![Ali\_Williams](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58f4c7/32.png) [@Ali\_Williams](https://www.ruby-forum.com/u/Ali_Williams)\
**Post date:** [July 14, 2006, 11:01pm UTC](https://www.ruby-forum.com/t/relationships-one-model-referencing-same-table-for-differen/98353/5 "2006-07-14T23:01:00Z")

</div>

No that’s easy then

class Cause \< ActiveRecord::Base  
belongs\_to :cause\_of\_death, :class\_name =\> “Code”, :foreign\_key =\>  
“cod\_id”  
belongs\_to :manner\_of\_death, :class\_name =\> “Code”, :foreign\_key =\>  
“mod\_id”  
end

@cause = Cause.find :first  
@cause.manner\_of\_death.description  
@cause.cause\_of\_death.description

---

<div class="post-metadata">

**Author:** ![Ali\_Williams](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58f4c7/32.png) [@Ali\_Williams](https://www.ruby-forum.com/u/Ali_Williams)\
**Post date:** [July 14, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/relationships-one-model-referencing-same-table-for-differen/98353/6 "2006-07-14T22:41:47Z")

</div>

Ok, I get what you’re saying here. Cause does have multiple codes, BUT  
they are  
of two different CODE TYPES: cause of death and manner of death. So  
what you’re  
saying about adding a code\_id to the cause table makes sense, but I’m  
working  
with two different data sets, so how can I differentiate between the  
two(cod\_id,  
mod\_id)? Maybe I need to concede to the fact that my code tables need  
to be  
broken out so I can property conform to what Rails wants.
