Mongoid and Embedded Relations

Date:

Tags:

I am experimenting with Mongoid and embedded relations. I have a class A that can have three named 1..1 relations to class B, and I found it quite problematic to get the reverse relationship setup. After specifying class_name and inverse_of everything started working as expected.

class B

  include Mongoid::Document

  embedded_in :a, class_name: "A"

end

class A

  include Mongoid::Document

  embeds_one :something, class_name: "B", inverse_of: :a
  embeds_one :completely class_name: "B", inverse_of: :a
  embeds_one :different, class_name: "B", inverse_of: :a

end