嵌套属性可以与继承结合使用吗?

发布于 2024-08-26 21:49:10 字数 704 浏览 9 评论 0原文

我有以下课程:

  • 项目
  • 人员
  • 人员 > 开发人员
  • 人员 > Manager

Project 模型中,我添加了以下语句:

has_and_belongs_to_many :people
accepts_nested_attributes_for :people

当然,还有 Person 类中的相应语句。如何通过 nested_attributes 方法将 Developer 添加到 Project?以下内容不起作用:

@p.people_attributes = [{:name => "Epic Beard Man", :type => "Developer"}]
@p.people
=> [#<Person id: nil, name: "Epic Beard Man", type: nil>]

如您所见,type 属性设置为 nil 而不是 "Developer"

I have the following classes:

  • Project
  • Person
  • Person > Developer
  • Person > Manager

In the Project model I have added the following statements:

has_and_belongs_to_many :people
accepts_nested_attributes_for :people

And of course the appropriate statements in the class Person. How can I add a Developer to a Project through the nested_attributes method? The following does not work:

@p.people_attributes = [{:name => "Epic Beard Man", :type => "Developer"}]
@p.people
=> [#<Person id: nil, name: "Epic Beard Man", type: nil>]

As you can see the type attributes is set to nil instead of "Developer".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鲜肉鲜肉永远不皱 2024-09-02 21:49:10

Rails3 解决方案attributes_protected_by_default 现在是一个类 -方法:

class Person < ActiveRecord::Base

  private

  def self.attributes_protected_by_default
    super - [inheritance_column]
  end
end

Solution for Rails3: attributes_protected_by_default in now a class-method:

class Person < ActiveRecord::Base

  private

  def self.attributes_protected_by_default
    super - [inheritance_column]
  end
end
凹づ凸ル 2024-09-02 21:49:10

几天前我遇到了类似的问题。 STI 模型中的继承列(即type)是受保护的属性。执行以下操作来覆盖 Person 类中的默认保护。

Rails 2.3

class Person < ActiveRecord::Base

private
  def attributes_protected_by_default
    super - [self.class.inheritance_column]
  end
end

Rails 3

请参阅 解决方案 href="https://stackoverflow.com/users/188031/tokland">@tokland。

警告:

您正在覆盖系统受保护的属性。

参考:

关于该主题的问题

I encountered a similar problem few days ago. The inheritance column(i.e. type) in a STI model is a protected attribute. Do the following to override the default protection in your Person class.

Rails 2.3

class Person < ActiveRecord::Base

private
  def attributes_protected_by_default
    super - [self.class.inheritance_column]
  end
end

Rails 3

Refer to the solution suggested by @tokland.

Caveat:

You are overriding the system protected attribute.

Reference:

SO Question on the topic

傲性难收 2024-09-02 21:49:10

上面的补丁对我不起作用,但这确实(Rails3):

class ActiveRecord::Reflection::AssociationReflection
  def build_association(*options)
    if options.first.is_a?(Hash) and options.first[:type].presence
      options.first[:type].to_s.constantize.new(*options)
    else
      klass.new(*options)
    end
  end
end

Foo.bars.build(:type=>'Baz').class == Baz

Patches above did not work for me, but this did (Rails3):

class ActiveRecord::Reflection::AssociationReflection
  def build_association(*options)
    if options.first.is_a?(Hash) and options.first[:type].presence
      options.first[:type].to_s.constantize.new(*options)
    else
      klass.new(*options)
    end
  end
end

Foo.bars.build(:type=>'Baz').class == Baz

飞烟轻若梦 2024-09-02 21:49:10

对于我们这些使用 Mongoid 的人来说,您需要使 _type 字段可访问:

class Person
  include Mongoid::Document
  attr_accessible :_type
end

For those of us using Mongoid, you will need to make the _type field accessible:

class Person
  include Mongoid::Document
  attr_accessible :_type
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文