【发布时间】:2011-10-28 20:35:59
【问题描述】:
在我的 Rails 应用程序中,Company 充当 用户模型。 公司可以有许多客户和许多雇员(汽车销售商)。
carseller 和 customer 之间存在多对多关系。
存在以下问题:很多时候,我必须检索与整个公司进行的所有约会。由于 appointment 模型中没有保存 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
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 database-design model entity-relationship