可能是一个令人困惑的标题,但不知道怎么回事,把它。 例如应使其更清晰。 我有很多共同的属性的许多不同的模式。 因此,在每个模型我必须指定这些相同的属性,然后是特定于特定模型的属性。
有没有什么办法可以创建一些类,列出了这些基本属性,然后从该类继承,而无需使用单表继承? 因为如果我把所有的共享属性和Mongoid包括成一个单一的模式,继承从其他型号的基础模型,然后STI被执行,我的所有车型都存储在一个单一的MongoDB集合,由“_type”字段分化。
这是我有:
class Model_1
include Mongoid::Document
field :uuid, :type => String
field :process_date, :type => String
...
end
class Model_2
include Mongoid::Document
field :uuid, :type => String
field :process_date, :type => String
...
end
但是,这是经过我的功能:
class Base_model
field :uuid, :type => String
field :process_date, :type => String
end
class Model_1 < Base_model
# To ensure STI is not enforced
include Mongoid::Document
# Attribute list inherited from Base_model
end
问题是,如果你没有在Base_model该“包括Mongoid ::文档”,然后是基本型号不知道“田......”的功能。 但是,如果你把mongoid包括在基本模型,并继承它,STI强制执行。
我不能为这种特殊的情况做STI但它是一个编码的噩梦有多个型号,全部采用指定的一遍又一遍相同的属性列表(也有越来越多的车型和15-20属性每股,所以我随时要改变这是一个很大的努力来改变它无处不在,...)的型号名称。