ActiveRecordに継承を格納する完全にサポートされている戦略は、STIです。ただし、具体的なクラステーブルの継承を自己責任でシミュレートすることはできます。抽象スーパークラスを使用した具体的なクラステーブル継承は、スマッシュで指摘されているように、うまく機能します。
しかし、AnotherSection(データベースに保存されない)普通のクラスにすると、(Veraticusの示唆しているように)ディスクリミネータカラムを無効にすることができます。ただし、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使用した場合も、それは破壊、オリジナルのインスタンス化をAnotherSectionを返します。 ()など()名前を付けて保存永続性操作を、オーバーライドして見つけること:
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
を一言で言えば、あなたはこれを行うことができますが、あなたSHOULDN'T。リスクは高いです。 継承の代わりにMIXINを使用するなど、別のオプションを検討する必要があります。
あなたが気にしてはいけない 'type'カラムがなければ...' type'を持っているなら、@Veraticusが言ったことをすることでそれを無効にすることができます。 – shuriu
実際にあなたはまだSTIを持っている:両方のクラスからのインスタンスが同じテーブルに格納される、STI(Single Table Inheritance)の定義は何か?あなたは、弁別子列( "型")を持つことは望ましくありません。ただし、セクションの各レコードがプレーン・セクションかAnotherSectionかどうかをどのように知るか? – atorres