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?
On 6/9/07, [email protected] [email protected] wrote:
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
–
F. Morgan Whitney
Morgan Whitney wrote the following on 09.06.2007 12:41 :
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
creates the:
model., model.=, …
accessors so you obviously don’t want several “belongs_to …”
with identical values of .
(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 B. wrote the following on 09.06.2007 12:55 :
referencing the same table. How can I do this?
class Program
belongs_to :home_team, :class_name => :team
better be :class_name => “Team”
Hi, guys! I’m back…Thanks for your feedback…it worked …great
advices from all of you…
On Jun 9, 8:28 pm, Josh S. [email protected]
[email protected] wrote:
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?
For non-standard association names, you have to specify both the foreign
key and the class name. Try this:
class Program
belongs_to :home_team,
:class_name => “Team”, :foreign_key => “home_team_id”
belongs_to :away_team,
:class_name => “Team”, :foreign_key => “away_team_id”
end
class Team
has_many :home_programs,
:class_name => “Program”, :foreign_key => “home_team_id”
has_many :away_programs,
:class_name => “Program”, :foreign_key => “away_team_id”
end
–
Josh S.
http://blog.hasmanythrough.com