私はRailsアプリケーションでSTIパターンを実装しようとしていますが、私は夢中になります。例えば、スーパークラスを呼び出して、私はサブクラスのインスタンスを取得したいRails 4.2 STI //間違ったキャスト(サブクラスの代わりにスーパークラス)
class ApartmentReservation < Reservation
#some code here
end
class StorageReservation < Reservation
#some code here
end
:
スーパークラス:
class Reservation < ActiveRecord::Base
self.inheritance_column = :inheritance_type
#some code here
end
サブクラス
は、私は三つのモデルを持っています
Reservation.all
は、ApartmentReservationとStorageReservationのコレクションを返す必要がありますが、予約のコレクションを返します。
私は答えを探していましたが、開発環境の負荷が大きかったことがわかりました。初期化子で
:
require_dependency 'app/models/apartment_reservation'
require_dependency 'app/models/storage_reservation'
:スーパークラスの下で
def self.descendants
[ApartmentReservation, StorageReservation]
end
:
スーパークラスでRails.configuration.to_prepare do
require_dependency 'app/models/apartment_reservation'
require_dependency 'app/models/storage_reservation'
end
アドバイスに続いて、私は、コードのこれらの部分をしようとしてきました
しかし、それでも動作しません。
誰もが考えていますが、どのように動作させるのですか?
rails server
に該当しますが、rails console
ではすべて正常です。
これらの方法でしたか? –
@SergioTulentsev最初のメソッドは状態の配列を返し、2番目は属性の配列です(ActiveRecordのcolumns_hashメソッドを使用していました)。 –
これらのメソッドを答えに入れるヒント(またはその署名)は最低限必要です。 –