2

Using Rails 3.2, and Mongoid 2.4. I have a legacy model, Organization, that embeds_many organization_members. It looks something like this:

class Organization
  include Mongoid::Document

  embeds_many :organization_members
end

class OrganizationMembers
  include Mongoid::Document
  embedded_in :organization
end

What I'd like to do is change the method I use to access members from organization.organization_members to just organization.members. Here's what I've done:

class Organization
  include Mongoid::Document

  embeds_many :members, class_name:"OrganizationMember"
end

class OrganizationMembers
  include Mongoid::Document
  embedded_in :organization
end

However, now organization.members returns an empty array and organization.organization_members returns the previous documents, even though it church_members isn't defined.

How do I persuade Mongoid to use the previous embedded collection name and access it through the new method call (Organization#members not Organization#organization_members)?

Glenn
  • 1,092
  • 1
  • 10
  • 22

1 Answers1

6

There's an option to embeds_many, called store_as.

class Organization
  include Mongoid::Document

  embeds_many :members, 
              class_name:"OrganizationMember", 
              store_as: 'organization_members'
end
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • Thanks! I tried that, and it responds with an Invalid Option error. Apparently, store_as is only relevant since Mongoid 3 (http://www.rdoc.info/github/mongoid/mongoid/master/Mongoid/Relations/Metadata:store_as). Do you know of a 2.4 equivalent for this? – Glenn Jul 27 '12 at 20:25
  • 1
    @Glenn: If I were stuck with 2.4, I'd rename the old field in the database. – Sergio Tulentsev Jul 27 '12 at 20:34
  • I guess its time to upgrade. I started looking at monkey patching for my use case, and it just seemed to make more sense to bite the bullet and get on Mongoid 3. Thanks for your input. Appreciate it. – Glenn Jul 27 '12 at 21:46