views:

20

answers:

1

I have 5 classes that are part of a point of sales program:
Customer, Technician, Order, Ticket, Service

Each Order has one customer and many tickets. Each Ticket has on technician and many services.

How should I make the associations so that I can do the following lookup:
customer.order.find_by_date(10/20/2010).service.technician.name

This is what I am planning:

class Customer < ActiveRecord::Base  
  has_many :orders
  has_many :tickets, :through => :orders
  has_many :technicians, :through => :tickets
end  

class Technician < ActiveRecord::Base
  belongs_to :ticket
  belongs_to :service
end  

class Service < ActiveRecord::Base
  has_many :technicians  
  belongs_to :ticket
end  

class Ticket < ActiveRecord::Base
  has_many :services
  has_many :technicians, :through => :services      
end  

class Order < ActiveRecord::Base
  has_many :tickets
  has_many :services, :through => tickets
  has_many :technicians, :through => services

  belongs_to :customer
end 
A: 

The relationship between customer and order is one-to-many. And the relationship between order and service is also one-to-many. Therefore you cannot do:

customer.order.find_by_date('10/20/2010').service.technician.name

It should be:

customer.orders.find_by_date('10/20/2010').services.each do |service|
  service.technician.name
end

It's a similar problem as in your other question.

captaintokyo