Let''s say I have a car table that has many passengers so obviously the passenger table will have a foreign key to car. The Car model has_many Passengers. A car can have one and only one driver. What''s the best way to model this relationship? Hand coded get and set driver methods in the car class with one of the following keys. 1) a flag in the passenger table noting the passenger as driver 2) a passenger foreign key in car to note driver Extend passenger to make a Driver model and do either: 3) Car belongs_to Driver 4) Car has_one Driver Or something else? Thanks! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I would do: Passengers belong_to Car Car has_many Passengers Car has_one Driver, :class => Passenger Dieter Lunn http://www.coder2000.ca On Mon, May 10, 2010 at 7:46 AM, John Sikorski <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Let''s say I have a car table that has many passengers so obviously the > passenger table will have a foreign key to car. The Car model has_many > Passengers. A car can have one and only one driver. What''s the best > way to model this relationship? > > Hand coded get and set driver methods in the car class with one of the > following keys. > 1) a flag in the passenger table noting the passenger as driver > 2) a passenger foreign key in car to note driver > > > Extend passenger to make a Driver model and do either: > > 3) Car belongs_to Driver > > 4) Car has_one Driver > > > Or something else? > > Thanks! > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mon, May 10, 2010 at 8:52 AM, Dieter Lunn <coder2000-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I would do: > > Passengers belong_to Car > Car has_many Passengers > Car has_one Driver, :class => Passenger >I''d do something similar. Actually if I had the freedom, I''d probably rename the class Passenger to something like Person since Driver and Passenger are really roles, not entities. class Person < ActiveRecord::Base belongs_to :car end class Car < ActiveRecord::Base belongs_to :driver, :class => ;person has_many :passengers, :class => person end -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.