Hey All, Forgive me for posting a question that may be very elementary. I''m building an application and am a bit stuck on how to work out the associations. It goes like this: User :has_many vehicles User :has_many logs Vehicle :has_many logs User -> Vehicles -> Logs The Vehicles have a user_id attribute. The Logs have user_id and vehicle_id attributes. I am using restful authentication and am able to associate the vehicles with the users al a ----- def create @vehicle = current_user.vehicle.build (params[:vehicle]) ...... ------ I can''t figure out how I should do it for the log. Thanks again, and sorry for my lack of understanding of how rails works. Cheers, Cam Peterson -- 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.
Manish Nautiyal
2010-May-10 09:14 UTC
Re: Ruby on Rails Associations - Practical Applications
Cam wrote:> Hey All, > Forgive me for posting a question that may be very elementary. > > I''m building an application and am a bit stuck on how to work out the > associations. It goes like this: > > User :has_many vehicles > User :has_many logs > > Vehicle :has_many logs > > User -> Vehicles -> Logs > > The Vehicles have a user_id attribute. > The Logs have user_id and vehicle_id attributes. > > I am using restful authentication and am able to associate the > vehicles with the users al a > ----- > def create > @vehicle = current_user.vehicle.build (params[:vehicle]) > ...... > ------ > > I can''t figure out how I should do it for the log. > Thanks again, and sorry for my lack of understanding of how rails > works. > > Cheers, > > Cam PetersonIn User model write has_many :logs has_many :vehicles In Vehicle model write has_many :logs In your logs model write belongs_to :user belongs_to :vehicle In your vehicle model write belongs_to :user def create @vehicle = current_user.vehicle.create (params[:vehicle]) -- 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.
Marnen Laibow-Koser
2010-May-10 14:35 UTC
Re: Ruby on Rails Associations - Practical Applications
Cam wrote:> Hey All, > Forgive me for posting a question that may be very elementary. > > I''m building an application and am a bit stuck on how to work out the > associations. It goes like this: > > User :has_many vehicles > User :has_many logs > > Vehicle :has_many logs > > User -> Vehicles -> Logs > > The Vehicles have a user_id attribute. > The Logs have user_id and vehicle_id attributes. > > I am using restful authentication and am able to associate the > vehicles with the users al a > ----- > def create > @vehicle = current_user.vehicle.build (params[:vehicle]) > ...... > ------ > > I can''t figure out how I should do it for the log. > Thanks again, and sorry for my lack of understanding of how rails > works.You probably want to use has_many :through. Have you read the Rails guide on associations?> > Cheers, > > Cam PetersonBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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.
Robert Walker
2010-May-10 20:13 UTC
Re: Ruby on Rails Associations - Practical Applications
Cam wrote:> I''m building an application and am a bit stuck on how to work out the > associations. It goes like this: > > User :has_many vehicles > User :has_many logs > > Vehicle :has_many logs > > User -> Vehicles -> Logs > > The Vehicles have a user_id attribute. > The Logs have user_id and vehicle_id attributes.I see two possible scenarios here. Neither is wrong, They depend on behavior you want. 1. Users have many vehicles and vehicles have many logs. 2. Users have many vehicles through logs. In scenario (1) you would need two one-to-many associations: User has_many :vehicles Vehicle belongs_to :user has_many :logs Log belongs_to :user Logs would now indirectly belong to a user because vehicles belong to users. Therefore Log would have only a foreign key to vehicle, but no foreign key to user since that would be redundant. Find the User for a Log with: user = my_log.vehicle.user In scenario (2) there would be a many-to-many association between User and Vehicle with Log joining them. User has_many :logs has_many :vehicles, :through => :logs Vehicle: has_many :logs has_many :users, :through => :logs Log belongs_to :user belongs_to :vehicle This scenario could be read as Users have many vehicles and vehicles can belong to many users, where logs track the association and can store information pertaining to a specific vehicle belonging to a specific user. I''m guessing this is not the scenario you want, but this pattern certainly has its uses. -- 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.