Hi everyone! I have a problem with defining 2 fk referencing the same table. I have a Program table and a Team table. The Program should have an away team and a home team fk. From my understanding, "the fk column should be named after the class of the target table, converted to lowercase, with _id appended". But, in my case, I have 2 fk referencing the same table. How can I do this?
I think this might work, but I didn't set up a DB or an app to test it
class Program belongs_to :team, :foreign_key => "home_team_id" belongs_to :team, :foreign_key => "away_team_id" end
Morgan Whitney wrote the following on 09.06.2007 12:41 :
Hi everyone! I have a problem with defining 2 fk referencing the same table. I have a Program table and a Team table. The Program should have an away team and a home team fk. From my understanding, "the fk column should be named after the class of the target table, converted to lowercase, with _id appended". But, in my case, I have 2 fk referencing the same table. How can I do this? I think this might work, but I didn't set up a DB or an app to test it
class Program belongs_to :team, :foreign_key => "home_team_id" belongs_to :team, :foreign_key => "away_team_id" end
class Program belongs_to :home_team, :class_name => :team belongs_to :away_team, :class_name => :team end
To avoid mistakes, just remember that
belongs_to <sym>
creates the:
model.<sym>, model.<sym>=, ...
accessors so you obviously don't want several "belongs_to <sym> ..." with identical values of <sym>.
(Note that this memento is mostly valid for other kinds of associations)
The foreign key should be computed automatically (by adding _id in this example), if it doesn't suits you, then you can use the :foreign_key parameter.
See ActiveRecord docs for more.
Lionel.
Lionel Bouton wrote the following on 09.06.2007 12:55 :
Morgan Whitney wrote the following on 09.06.2007 12:41 :
Hi everyone! I have a problem with defining 2 fk referencing the same table. I have a Program table and a Team table. The Program should have an away team and a home team fk. From my understanding, "the fk column should be named after the class of the target table, converted to lowercase, with _id appended". But, in my case, I have 2 fk referencing the same table. How can I do this?
I think this might work, but I didn't set up a DB or an app to test it
class Program belongs_to :team, :foreign_key => "home_team_id" belongs_to :team, :foreign_key => "away_team_id" end
class Program belongs_to :home_team, :class_name => :team better be :class_name => "Team"
Thanks guys... I will try it out and get back to you...
We manufacture our own exclusive replica so we oversee the production of each handmade item. Each item is inspected before shipment to ensure the highest quality standards. Each handbag comes with all the accessories included in the original item such as tags, straps, etc The pictures on our site are of a random sampling of our stock. The item you will receive will be identical to the item in the picture.Quality is our number one priority. We do not manufacture an item until we have checked the original for all the correct markings and materials. Our company specializes in replicating designer handbags. All our products are made with the same materials as the original handbags. They are identical in every way right down to the serial numbers embossed on each item. http://www.replica-supplier.com
Hi, guys! I'm back...Thanks for your feedback...it worked ...great advices from all of you...