The relationship is invalid because when you reference the built-in model, Mongoid does not save the parent key as a foreign key. This means that if you have:
Class Parent embeds_many :children end Class Child embedded_in :parent end
You cannot reference a child document storing only its foreign key, but you need to save all the keys of the parents until you reach the root . In this case, the root is represented by the first parent, and you need to save 2 keys.
You can do this manually and create such an association without any problems.
Your case is slightly different (and simpler) because you want to create a relationship between two models embedded in the same parent. This means that theoretically you do not need to store the parent key, because the models have the same root. Mongoid cannot handle this scenario, so you need to manually create your association rules and methods.
Class Bar embeds_many :beers embeds_many :glasses end Class Beer embedded_in :bar
(code not verified)
source share