Skip to content
tmaier edited this page Nov 2, 2011 · 2 revisions

This is a collection of hints and tips which are not (yet) documented at mongoid.org.
mongoid.org shall always be the first reference, however If something is worth being noted, this could be a good starting point.

Associations with nested models

Ref. #1207

1. Reference classes of another module with the :class_name attribute.

module ItchySpot
  class Dog
    include Mongoid::Document

    embeds_one :collar
  end
end

class Collar
  include Mongoid::Document

  embedded_in :dog, class_name: "ItchySpot::Dog"
end

2. Classes within the same module require a :class_name attribute as well

module ItchySpot
  class Dog
    include Mongoid::Document

    embeds_one :collar, class_name: "::Collar"
  end

  class Collar
    include Mongoid::Document

    embedded_in :dog, class_name: "::Dog"
  end
end