开发者

What's the best way to make Rails 3 / Active Record associations for classes of a simple point of sale program?

开发者 https://www.devze.com 2023-01-20 23:15 出处:网络
I have 5 classes that are part of a point of sales program: Customer, Technician, Order, Ticket, Service

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 :se开发者_如何学Pythonrvices
  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 


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消