我有不同的模块,例如绘画,chasis_repair,over_oiling 部分每个模块都有单独的账单表,例如painting_bills,chasis_repair_bills,over_oiling_bills
现在我必须为 Delivery 模块创建账单。交付模块与其他模块有关系(涂装、底盘维修、过油)
注意:喷漆、底盘修理、过油部分账单和交付特定账单是可选的。
示例 1:交货单包含喷漆和底盘维修账单 示例 2:交货单包含过度涂油、喷漆和交货特定账单
class DeliveryBill < ActiveRecord::Base
attr_accessible :date, :total_amount, :discount
has_many :delivery_detail_bills
end
我对构建delivery_detail_bills 模型感到困惑,我有两个想法
1)
id
date
tot_amount
bill_item_type # name of section as text for example painting/chasis_repair/over_oiling/other
bill_item_amount # for example, respective total amount of painting/chasis_repair/over_oiling/other
bill_item_klass # for example, here I store the respective class name, PaintingBill, ChasisRepairBill...
bill_item_klass_id # id value of respective object
这样就没有关系表了。我通过 bill_item_type 列处理交付特定计费项目如果我想去相应的账单,我必须使用 bill_item_klass、bill_item_klass_id 列手动建立相应的链接
2)创建has_many:通过delivery_bill与painting_bills、chasis_repair_bills之间的关联表
class DeliveryBill < ActiveRecord::Base
has_many :painting_bills, :through => :delivery_bill_painting_bills
has_many :chasis_repair_bills, :through => :delivery_bill_chasis_repair_bills
.....
end
class DeliverBillPaintingBill < ActiveRecord::Base
belongs_to :delivery_bill
belongs_to :painting_bill
end
class DeliverBillChasisRepairBill < ActiveRecord::Base
belongs_to :delivery_bill
belongs_to :chasis_repair_bill
end
....
这里的关系很清楚,但如果我按照这种方式,我无法处理“其他”的账单,因为没有额外/其他账单的关联表。此方法将增加表数
哪一个是性能方面的最佳解决方案,还是有其他更好的解决方案?