Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-991

Embedding objects of the same class by different names

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.1.9
    • Affects Version/s: None
    • Component/s: None
    • None

      Consider the following:

      
      Mongoid::Config.settings = {
        "host" => "127.0.0.1",
        "database" => "testing"
      }
      
      class Item
        include Mongoid::Document
        embedded_in :list
      
        field :name
      
        def who_am_i
          puts metadata.name
        end
      end
      
      
      class List
        include Mongoid::Document
        embeds_many :cool_items, :class_name => 'Item'
        embeds_many :simple_items, :class_name => 'Item'
      
        field :name
      end
      
      
      List.delete_all
      
      list = List.create!
      list.cool_items.create!(:name => 'cool')
      list.simple_items.create!(:name => 'uncool')
      
      cool_item = list.cool_items.first
      cool_item.who_am_i
      
      simple_item = list.simple_items.first
      simple_item.who_am_i
      
      

      Gives:
      <pre>
      MONGODB testing['lists'].update(

      {"_id"=>BSON::ObjectId('4df7f346b4e4cf3b41000001')}

      , {"$push"=>{"cool_items"=>

      {"_id"=>BSON::ObjectId('4df7f346b4e4cf3b41000002'), "name"=>"cool"}

      }})
      MONGODB testing['lists'].update(

      {"_id"=>BSON::ObjectId('4df7f346b4e4cf3b41000001')}

      , {"$push"=>{"cool_items"=>

      {"_id"=>BSON::ObjectId('4df7f346b4e4cf3b41000003'), "name"=>"uncool"}

      }})
      cool_items
      cool_items
      </pre>

      It can be partially worked-around if :inverse_of is added, but then it's impossible to use "CoolItem#list" method.

            Assignee:
            Unassigned Unassigned
            Reporter:
            romanbsd Roman
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: