agolaru-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jun-09 09:42 UTC
create 2 fk referencing the same table
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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 6/9/07, agolaru-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <agolaru-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 http://www.blizzo.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Morgan Whitney wrote the following on 09.06.2007 12:41 :> On 6/9/07, agolaru-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <agolaru-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > >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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Lionel Bouton wrote the following on 09.06.2007 12:55 :> Morgan Whitney wrote the following on 09.06.2007 12:41 : > >> On 6/9/07, agolaru-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <agolaru-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 >> >> >> > > class Program > belongs_to :home_team, :class_name => :team >better be :class_name => "Team" --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
agolaru-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jun-09 11:27 UTC
Re: create 2 fk referencing the same table
Thanks guys... I will try it out and get back to you... On Jun 9, 1:56 pm, Lionel Bouton <lionel-subscript...-WTamNBQcZIx7tPAFqOLdPg@public.gmane.org> wrote:> Lionel Bouton wrote the following on 09.06.2007 12:55 : > > > > > Morgan Whitney wrote the following on 09.06.2007 12:41 : > > >> On 6/9/07, agol...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <agol...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > > class Program > > belongs_to :home_team, :class_name => :team > > better be :class_name => "Team"--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
agolaru-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 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 Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
agolaru-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jun-09 19:51 UTC
Re: create 2 fk referencing the same table
Hi, guys! I''m back...Thanks for your feedback...it worked ...great advices from all of you... On Jun 9, 8:28 pm, Josh Susser <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> agol...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 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 Susserhttp://blog.hasmanythrough.com > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---