2012-07-27 41 views
2

使用Rails 3.2和Mongoid 2.4。我有一个遗留模型组织,即embeds_many organization_members。它看起来是这样的:更改mongoid embeds_many协会名称

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#organization_members)?

回答

6

有一个选项embeds_many,称为store_as

class Organization 
    include Mongoid::Document 

    embeds_many :members, 
       class_name:"OrganizationMember", 
       store_as: 'organization_members' 
end 
+0

谢谢!我试过了,它以无效选项错误作出响应。显然,store_as只有在Mongoid 3之后才有意义(http://www.rdoc.info/github/mongoid/mongoid/master/Mongoid/Relations/Metadata:store_as)。你知道这个2.4等值吗? – Glenn 2012-07-27 20:25:07

+1

@Glenn:如果我坚持2.4,我会重命名数据库中的旧字段。 – 2012-07-27 20:34:13

+0

我猜是时候升级了。我开始为我的使用案例寻找猴子修补程序,似乎咬住子弹并使用Mongoid 3更有意义。感谢您的输入。欣赏它。 – Glenn 2012-07-27 21:46:56