Her*_* D. 7 inheritance ruby-on-rails single-table-inheritance nested-attributes
我有以下课程:
在Project模型中,我添加了以下语句:
has_and_belongs_to_many :people
accepts_nested_attributes_for :people
Run Code Online (Sandbox Code Playgroud)
当然还有课堂上适当的陈述Person.如何通过方法添加Developer到a ?以下不起作用:Projectnested_attributes
@p.people_attributes = [{:name => "Epic Beard Man", :type => "Developer"}]
@p.people
=> [#<Person id: nil, name: "Epic Beard Man", type: nil>]
Run Code Online (Sandbox Code Playgroud)
如您所见,type属性设置为nil而不是"Developer".
Rails3的解决方案:attributes_protected_by_default现在是一个类方法:
class Person < ActiveRecord::Base
private
def self.attributes_protected_by_default
super - [inheritance_column]
end
end
Run Code Online (Sandbox Code Playgroud)
我前几天遇到过类似的问题.typeSTI模型中的继承列(即)是受保护的属性.执行以下操作以覆盖类中的默认保护Person.
Rails 2.3
class Person < ActiveRecord::Base
private
def attributes_protected_by_default
super - [self.class.inheritance_column]
end
end
Run Code Online (Sandbox Code Playgroud)
Rails 3
警告:
您正在覆盖系统保护的属性.
参考:
| 归档时间: |
|
| 查看次数: |
2771 次 |
| 最近记录: |