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
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