我的 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
字段。
我希望有比这更好的方法。