次のようにSTIを実装しています。
class Automobile < ActiveRecord::Base
end
class Car < Automobile
end
class Truck < Automobile
end
class User < ActiveRecord::Base
has_many :automobiles
accepts_nested_attributes_for :automobiles
end
ユーザーの自動車のリストを作成しています。自動車ごとに、UI は自動車にtype
関連付けられたフィールドとプロパティを設定します。フォームの送信中、このtype
フィールドは保護された属性であるため無視されます。
この問題を回避するにはどうすればよいですか? unprotect
保護された属性への宣言的な方法はありますか?
編集:
これは問題に対する私の現在の解決策です:attributes_protected_by_default
モデルクラスのプライベートメソッドをオーバーライドします。
class Automobile < ActiveRecord::Base
private
def attributes_protected_by_default
super - [self.class.inheritance_column]
end
end
type
これにより、フィールドが保護リストから削除されます。
これよりも良い方法があることを願っています。