Kintarō
Kintarō

Reputation: 3187

MongoId: How to specify relationship correctly

I am new to MongoId. I saw the document by using embeds_many and embedded_in. Just wonder do I need to use it as a pair?

class Band
  include Mongoid::Document
  embeds_many :albums
end

class Album
  include Mongoid::Document
  field :name, type: String
  embedded_in :band
end

Thanks

Upvotes: 0

Views: 42

Answers (1)

mu is too short
mu is too short

Reputation: 434945

The fine manual is fairly clear on this:

The parent document of the relation should use the embeds_many macro to indicate it has n number of embedded children, where the document that is embedded uses embedded_in.
[...]

Definitions are required on both sides to the relation in order for it to work properly.

So yes, you need both or it won't work properly.

Upvotes: 1

Related Questions