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

相关问题