# Difficulty using correct moldel for the data in DB

**URL:** <https://www.ruby-forum.com/t/difficulty-using-correct-moldel-for-the-data-in-db/54079>\
**Category:** Rails\
**Created:** [December 19, 2005, 5:32pm UTC](https://www.ruby-forum.com/t/difficulty-using-correct-moldel-for-the-data-in-db/54079 "2005-12-19T17:32:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcin\_Marjanski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b77776/32.png) [@Marcin\_Marjanski](https://www.ruby-forum.com/u/Marcin_Marjanski)\
**Post date:** [December 19, 2005, 5:32pm UTC](https://www.ruby-forum.com/t/difficulty-using-correct-moldel-for-the-data-in-db/54079/1 "2005-12-19T17:32:58Z")

</div>

Hi everyone,

I’ve tried posting the same question to the list last Friday but  
somehow did not see it being posted…

I’ve just started with Rails and Ruby. I just started to play one of  
those RPG and to get my feet wet I’m writing an app to keep track of  
item fusions. So the scoop is that item can be fused with another  
items to create a higher level item. The higher level items can be  
fused to create even higher level items … etc.

I’m using MySQL to store the data. Here is how I have setup the  
database:

CREATE TABLE `items` (  
`id` INTEGER(11) NOT NULL AUTO\_INCREMENT,  
`name` VARCHAR(255) ,  
`cat_id` INTEGER(11),  
`rank` INTEGER ,  
PRIMARY KEY(`id`)  
);

CREATE TABLE `cats` (  
`id` INTEGER(11) NOT NULL AUTO\_INCREMENT,  
`name` VARCHAR(255),  
PRIMARY KEY(`id`)  
);

CREATE TABLE `fusions` (  
`id` INTEGER(11) NOT NULL AUTO\_INCREMENT,  
`item1_id` INTEGER(11),  
`item2_id` INTEGER(11) ,  
`product_id` INTEGER(11),  
PRIMARY KEY(`id`)  
);

Now… Creating one-to-many relationship for item/category is cake.  
How do I set up my model for item and fusion so I can have something  
like below in my view?

\<% for fusion in @item.fusions %\>  
\<%= fusion.item1.name %\> + \<%= fusion.item2.name %\>  
\<% end %\>

BTW I’ve tried

CREATE TABLE `fusions` (  
`item1_id` INTEGER(11),  
`item2_id` INTEGER(11) ,  
`product_id` INTEGER(11),  
PRIMARY KEY(`item1_id`,`item2_id`)  
);

to have has\_and\_belongs\_to\_many relationship but it did not work. I  
think it has to do with join table having only the id’s for the items  
that are joined.

I’ve also tried to use info from  
[http://wiki.rubyonrails.org/rails/pages/HowToUseManyToManyAgainstASingleTable](http://wiki.rubyonrails.org/rails/pages/HowToUseManyToManyAgainstASingleTable)  
but was unsuccessful.

Because I was unable to come up with a good model for item/fusion and  
to limit my database usage, I’m resorting to having controller send  
fusions and items ordered by id and doing the following in my view.

\<% for fusion in @fusions %\>  
\<% item1 = @Items[fusion.item1\_id.to\_i - 1]  
\<% item2 = @Items[fusion.item2\_id.to\_i - 1]

\<%= item1.name %\> + \<%= item2.name %\>  
\<% end %\>

Any ideas?

Marcin

---

<div class="post-metadata">

**Author:** ![Marcin\_Marjanski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b77776/32.png) [@Marcin\_Marjanski](https://www.ruby-forum.com/u/Marcin_Marjanski)\
**Post date:** [December 19, 2005, 6:42pm UTC](https://www.ruby-forum.com/t/difficulty-using-correct-moldel-for-the-data-in-db/54079/2 "2005-12-19T18:42:10Z")

</div>

forgive me if i don’t understand correctly, but i think habtm is the  
answer,  
using self-referential joins.

[Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowToCreateASelfReferentialManyToManyRelationship)(scan  
down to 2nd half of page “A Slight Alternative”)

---

<div class="post-metadata">

**Author:** ![Marcin\_Marjanski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b77776/32.png) [@Marcin\_Marjanski](https://www.ruby-forum.com/u/Marcin_Marjanski)\
**Post date:** [December 22, 2005, 1:09am UTC](https://www.ruby-forum.com/t/difficulty-using-correct-moldel-for-the-data-in-db/54079/3 "2005-12-22T01:09:21Z")

</div>

Thanks Chirs,

Unless I’m doing something wrong I getting the following error

Showing app/views/item/show.rhtml where line #32 raised:  
undefined method `product’ for #Item:0x39460c8  
Extracted source (around line #32):

29: \<% for f in @item.fusions %\>  
30:   
31: \<%= h f.id -%\>   
32: \<%= h f.product\_id -%\> \<%= h f.product.name -%\>   
33: \<%= h f.item1\_id -%\>   
34: \<%= h f.item2\_id -%\>   
35:

My item.rb:

class Item \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :fusions,  
:class\_name =\> ‘Item’,  
:join\_table =\> ‘fusions’,  
:foreign\_key =\> ‘product\_id’,  
:association\_foreign\_key =\> ‘id’

end

How do I associate item1\_id, item2\_id and product\_id in the fusions  
table to Item?  
I don’t get the error when I update line 32 of show.rhtml to:

32: \<%= h f.product\_id -%\>

I’ve also experimented with creating habtm table with Item\_id and  
fusion\_id and fusions table that only has id, item1\_id and item2\_id.  
I found that using  
[http://wiki.rubyonrails.com/rails/pages/HowToCreateASelfReferentialManyToManyRelationship](http://wiki.rubyonrails.com/rails/pages/HowToCreateASelfReferentialManyToManyRelationship)  
I’m displaying only portions of fusions (only once where fusion\_id is  
less than max item\_id)

Any ideas?
