Search code examples
ruby-on-railsactiverecordassociationshas-manybelongs-to

Rails 3.0 has_many and belongs_to associations headache.


Currently learning Rails, so be gentle.

I want to check that i'm getting this right before I go to far:

Building an app that allows our 'Customers' to log in, and create a quote. 'Suppliers' (in this case conference venue owners) can then look a the quotes, and reply to them with a proposal, which the customer will be able to view. Each Supplier account will have the option to have one or more 'Venues' that belong to it (if they run a chain of venues for instance), and each proposal made will be from a specific venue.

Other complicated issues that I will probably come across later on, does this look right as far as the relationships go?

P.S I realise that the below is not actually code that will work, I just laid it out like this whilst I attempt to get my head around it.

Customer (will be a type of user)
has_many :quotes
has_many :proposals, :through => :venue

Supplier (will be a type of user)
has_many :venues
has_many :proposals, :through => :venue

Venue
belongs_to :supplier 
has_many Proposals

Quote
belongs_to :customer

Proposal
belongs_to :venue

And the basic tables:

Customer
    id

Supplier
    id

Quote
    id
    customer_id

Venue
    id
    supplier_id

Proposal
    id
    venue_id

There might be a much better way to do this, using has_ones and has_and_belongs_to_many etc, but i've no idea.

Thanks


Solution

  • Ok, so I think it should look like this:

    Customer
      has_many :quotes
      has_many :proposals
    
    Supplier
      has_many :venues
      has_many :proposals, :through => :venues
    
    Venue
      belongs_to :supplier 
      has_many :proposals
    
    Quote
      belongs_to :customer
    
    Proposal
      belongs_to :venue
      belongs_to :customer
    

    and in db schema:

    Customer
      id
    
    Supplier
      id
    
    Quote
      id
      customer_id
    
    Venue
      id
      supplier_id
    
    Proposal
      id
      venue_id
      customer_id