Topic: db structure question

So I currently have a very simple db structure. There are two tables "Users" and "Cars".

A user has_many cars and a car_belongs to a user.

I want to set up a new table for "Organization" (btw - this will eventually will be added to a subdomain a la 37's basecamp).

My question is on the theory for how to set up the models so...

An organization will have_many users and a user will belong_to an organization. Do I need to do the same for "cars"? A car cannot belong_to more than one organization but since it already belongs to a user, do I need to explicitly describe that in the model?

I guess my question is a bit ambiguous, but wanted to get some insite on if I should use habtm, has_many_through, polymorphic etc.

hope the above is somewhat answerable...


Last edited by seandevs (2011-04-27 09:08:56)

Re: db structure question

I would need more info about your app to answer questions like if cars are in any way related to organizations.  For instance would you ever need to ask "which cars belong to this organization?"
You could definitely use a has_many through relation if the answer to that question is yes

class Organization < ActiveRecord::Base
  has_many :users
  has_many :cars, :through => :users

should be that straight forward, now you can directly get cars from an organization instance

o = Organization.find(some_id); puts


Re: db structure question

Hi Aaron,

Brilliant. Thanks for the help. Very straight forward answer - really appreciate it.