2

使用 Rails 3.2 和 Mongoid 2.4。我有一个遗留模型,Organization,它嵌入了_many_organization_members。它看起来像这样:

class Organization
  include Mongoid::Document

  embeds_many :organization_members
end

class OrganizationMembers
  include Mongoid::Document
  embedded_in :organization
end

我想做的是将用于访问成员的方法从organization.organization_members更改为仅organization.members。这是我所做的:

class Organization
  include Mongoid::Document

  embeds_many :members, class_name:"OrganizationMember"
end

class OrganizationMembers
  include Mongoid::Document
  embedded_in :organization
end

但是,现在 organization.members 返回一个空数组,并且 organization.organization_members 返回以前的文档,即使它没有定义 Church_members。

如何说服 Mongoid 使用之前嵌入的集合名称并通过新方法调用(Organization#members 不是 Organization#organization_members)访问它?

4

1 回答 1

6

有一个选项embeds_many,称为store_as

class Organization
  include Mongoid::Document

  embeds_many :members, 
              class_name:"OrganizationMember", 
              store_as: 'organization_members'
end
于 2012-07-27T20:20:26.273 回答