我有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