在 ActiveRecord 上存储继承的唯一完全支持的策略是 STI。但是,您可以自担风险模拟具体的类表继承。正如 smathy 所指出的,具有抽象超类的具体类表继承工作正常。
但是...如果您想要使AnotherSection只是一个普通类(不会在数据库中持久化),您可以禁用鉴别器列(如 Veraticus 所建议的那样)。但是,如果您保存AnotherSection,它将与 Section 保存在同一个表中,您将无法区分它们。此外,如果您使用AnotherSection查找Section,它将返回AnotherSection,从而破坏原始实例化:
#create a Section and saves it
sect = Section.create()
sect.save()
#retrieve the Section as a AnotherSection, breaking polymorphism...
sect = AnotherSection.find(sect.id)
# another section is more than a section, it is inconsistent.
如果AnotherSection不打算被持久化,它覆盖持久化操作的最安全路径,例如 save() 和 find():
class AnotherSection < Section
# disable STI, as pointed by Veraticus
self.inheritance_column = :_type_disabled
# disable save and finding
def save(*args)
#exception? do nothing?
end
def find(*args)
#exception? do nothing?
end
def find_by(*args)
#exception? do nothing?
end
# this does not stops here! there is first, last, and even a forty_two finder method! not to mention associations...
end
简而言之,你可以这样做,但你不应该这样做。风险很高。您应该考虑另一种选择,例如使用 MIXIN 而不是继承。