我有以下类(class):
在
Project
模型我添加了以下语句:has_and_belongs_to_many :people
accepts_nested_attributes_for :people
当然还有类中的适当语句
Person
.如何添加 Developer
到 Project
通过 nested_attributes
方法?以下不起作用:@p.people_attributes = [{:name => "Epic Beard Man", :type => "Developer"}]
@p.people
=> [#<Person id: nil, name: "Epic Beard Man", type: nil>]
如您所见
type
属性设置为 nil
而不是 "Developer"
.
最佳答案
Rails3 的解决方案 :attributes_protected_by_default现在在一个类方法中:
class Person < ActiveRecord::Base
private
def self.attributes_protected_by_default
super - [inheritance_column]
end
end
关于ruby-on-rails - 嵌套属性可以与继承结合使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2553931/