3

在我的Rails應用程序公司充當用戶模型。一個公司可以有很多客戶和許多員工(carseller)。什麼是正確的導軌模型?使用冗餘推薦?

汽車零售商客戶之間存在多對多關係。

存在以下問題:很多時候我必須檢索與整個公司進行的所有約會。由於在約會模型中沒有保存company_id,所以這可能是相當痛苦的。

我應該只是包括任命一個外鍵公司,並有某種形式的冗餘或有另一種簡單而有效的方法是什麼?

class Company < ActiveRecord::Base 
    has_many :customers 
    has_many :carseller 
end 

class Customer < ActiveRecord::Base 
    belongs_to :company 

    has_many :appointments 
    has_many :carsellers, :through => :appointments 
end 

class Carseller < ActiveRecord::Base 
    belongs_to :company 

    has_many :appointments 
    has_many :customers, :through => :appointments 
end 

class Appointment < ActiveRecord::Base 
    belongs_to :customer 
    belongs_to :carseller 
end 

回答

1

我認爲你可以使用:

class Appointment < ActiveRecord::Base 
    belongs_to :customer 
    belongs_to :carseller 
    has_one :company, :through => :carseller 
end 

然後你只需要做appointment.company得到它。

+0

謝謝。但更重要的是另一個方向:獲取屬於某個公司的所有約會 – Ben 2011-08-17 12:07:36

相關問題