ruby-on-rails – 嵌套属性可以与继承结合使用吗?
发布时间:2020-12-17 04:25:38 所属栏目:百科 来源:网络整理
导读:我有以下课程: 项目 人 人开发人员 人经理 在Project模型中,我添加了以下语句: has_and_belongs_to_many :peopleaccepts_nested_attributes_for :people 当然还有Person类中的相应语句.如何通过nested_attributes方法将Developer添加到项目中?以下不起作
我有以下课程:
>项目 在Project模型中,我添加了以下语句: has_and_belongs_to_many :people accepts_nested_attributes_for :people 当然还有Person类中的相应语句.如何通过nested_attributes方法将Developer添加到项目中?以下不起作用: @p.people_attributes = [{:name => "Epic Beard Man",:type => "Developer"}] @p.people => [#<Person id: nil,name: "Epic Beard Man",type: nil>] 如您所见,类型属性设置为nil而不是“Developer”. 解决方法
我前几天遇到过类似的问题. STI模型中的继承列(即类型)是受保护的属性.执行以下操作以覆盖Person类中的默认保护.
Rails 2.3 class Person < ActiveRecord::Base private def attributes_protected_by_default super - [self.class.inheritance_column] end end Rails 3 请参阅@tokland建议的solution. 警告: 您正在覆盖系统保护的属性. 参考: SO Question on the topic (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |