Forum: Ruby on Rails Difficulty with creatating a model

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Marcin Marjanski (Guest)
on 2005-12-18 03:02
(Received via mailing list)
Hi everyone,

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/HowToUseMa...
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
This topic is locked and can not be replied to.