我有STI執行如下命令:如何覆蓋attr_protected?
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
我創建了一個用戶的汽車的列表。對於每輛汽車,用戶界面設置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
字段。
我希望有比這更好的方法。
我試過這個,但顯然Automobile.new(params [:汽車])不起作用,在我的創建控制器行動。 – 2011-05-18 15:00:12