james_027
2007-Jan-30 09:45 UTC
How to Model a table with two foreign keys of the same table
Hi the following table JobOrder: fields(id, date_filed, date_needed, request_by, process_by, details, date_finished) Employee(id, name) the request_by and the process_by field are the foreign key of the table emplyee. How will the model look like? class Joborder --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
james_027
2007-Jan-30 09:49 UTC
How to Model a table with two foreign keys of the same table
Hi the following table
JobOrder: fields(id, date_filed, date_needed, request_by, process_by,
details, date_finished)
Employee(id, name)
the request_by and the process_by field are the foreign key of the
table emplyee.
How will the model look like?
class Joborder < ActiveRecord::Base
belongs_to :employee, :class_name => "Employee", :foreign_key =
>
"request_by"
belongs_to :employee, :class_name => "Employee", :foreign_key
=>
"process_by"
end
Is the model above the correct way?
Thanks
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
P. Pantouffe
2007-Jan-30 09:53 UTC
Re: How to Model a table with two foreign keys of the same t
i think they should have different names ...... belongs_to :employee1, ..... belongs_to :employee2, ...... .... the mapping is done through the class_name and the foreign_key -- 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 -~----------~----~----~----~------~----~------~--~---
Alan Francis
2007-Jan-30 10:03 UTC
Re: How to Model a table with two foreign keys of the same t
P. Pantouffe wrote:> i think they should have different names > ...... > belongs_to :employee1, ..... > belongs_to :employee2, ...... > .... > the mapping is done through the class_name and the foreign_keyOnly thing I''d add is more sensible variable names (which is implied, I think). class Joborder < ActiveRecord::Base belongs_to :requestor, :class_name => "Employee", :foreign_key = > "request_by" belongs_to :processor, :class_name => "Employee", :foreign_key => "process_by" end This will let you do @job_order.processor.... or @job_order.requestor... A. -- 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 -~----------~----~----~----~------~----~------~--~---