[英]Rails Reverse Polymorphic Association
I am building a fee module for one of the school. 我正在为其中一所学校建立一个收费模块。 To make the fee module work, I want to define the fee templates (structure) which will have different types of fees (admission fee, library fee, sports fee etc), various discounts and any tax applicable. 为了使费用模块工作,我想定义费用模板(结构),它将有不同类型的费用(入场费,图书馆费,体育费等),各种折扣和任何适用的税收。 The fees, discounts and taxes are defined as different models as they contain different properties. 费用,折扣和税收被定义为不同的模型,因为它们包含不同的属性。
Here are the various models ... 以下是各种型号......
First the fee templates 首先是费用模板
class FeeTemplate < ActiveRecord::Base
has_many :fee_template_particulars
end
and then the FeeParticulars .. 然后是FeeParticulars ..
class FeeParticular < ActiveRecord::Base
belongs_to :fee_template
belongs_to :particulars, :polymorphic => true
end
Now fee, discount and tax model 现在是费用,折扣和税收模式
class Fee < ActiveRecord::Base
has_many :fee_particulars, :as => particulars
end
class Discount < ActiveRecord::Base
has_many :fee_particulars, :as => particulars
end
class Tax < ActiveRecord::Base
has_many :fee_particulars, :as => particulars
end
Essentially, database table for FeeTemplateParticular will have a type column to store the type of fee/discount/tax and the id record. 从本质上讲,FeeTemplateParticular的数据库表将有一个类型列来存储费用/折扣/税的类型和id记录。
Wanted to ask you guys few questions on the scenario I have to implement .. 想要问你们关于我必须实施的方案的几个问题。
Is the approach (polymorohic) good for this situation? 方法(多态)是否适合这种情况?
If above is okay and knowing the polymorphic association works one way but in my case, I will have to find the details of each type fee/discount/tax from the fee particulars and not the other way around. 如果上面的情况还可以,并且知道多态关联的工作方式是一种方式,但在我的情况下,我将不得不从费用细节中找到每种类型费用/折扣/税收的详细信息,而不是相反。 How would that be done? 怎么办?
Is there any better way to solve this problem. 有没有更好的方法来解决这个问题。
Thanks in advance. 提前致谢。
Yes this is good. 是的,这很好。
You have the field_particular for the has_many to find the particulars of different types. 你有has_many的field_particular来查找不同类型的细节。 You will be able to have different classes for the different behavior of the particulars. 您将能够针对细节的不同行为设置不同的课程。 I think this is a very good design for your problem. 我认为这是一个非常好的问题设计。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.