我的 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
受保护的属性?
Edit:这是我目前的问题解决方案:
我覆盖attributes_protected_by_default
我的模型类中的私有方法。
class Automobile < ActiveRecord::Base
private
def attributes_protected_by_default
super - [self.class.inheritance_column]
end
end
这删除了type
受保护列表中的字段。
我希望有比这更好的方法。
我最终这样做了:
class Automobile < ActiveRecord::Base
private
def attributes_protected_by_default
super - [self.class.inheritance_column]
end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)