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

Embedded docs' HABTM relations explode

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

      I just upgraded an app from 2.1.0 to 2.2.0 and discovered that if an embedded doc has a HABTM relation, the relation blows up if you try to set it.

      It looks like we need @cgriego's proposed belongs_to_many relation!

      
      class Order
        include Mongoid::Document
      end
      
      class User
        include Mongoid::Document
        embeds_many :addresses
      end
      
      class Address
        include Mongoid::Document
        embedded_in :user
        has_and_belongs_to_many :orders, :inverse_of => nil
      end
      
      Order.delete_all
      User.delete_all
      
      Order.create
      => #<Order _id: 4e6550dd2b58af32f2000007, _type: nil>
      
      User.create :addresses => [{:orders => Order.first}]
      Mongoid::Errors::InvalidCollection: Access to the collection for Address is not allowed since it is an embedded document, please access a collection from the root document.
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/collections.rb:55:in `collection'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/collections.rb:26:in `collection'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/persistence/atomic/operation.rb:20:in `collection'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/persistence/atomic/set.rb:22:in `block (2 levels) in persist'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/persistence/atomic/set.rb:21:in `tap'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/persistence/atomic/set.rb:21:in `block in persist'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/persistence/atomic/operation.rb:78:in `prepare'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/persistence/atomic/set.rb:19:in `persist'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/persistence/atomic.rb:204:in `set'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/relations/referenced/many_to_many.rb:146:in `nullify'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/relations/referenced/many.rb:275:in `block in substitute'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/relations/referenced/many.rb:273:in `tap'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/relations/referenced/many.rb:273:in `substitute'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/relations/accessors.rb:126:in `block (2 levels) in setter'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/attributes/processing.rb:138:in `block in process_relations'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/attributes/processing.rb:133:in `each_pair'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/attributes/processing.rb:133:in `process_relations'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/attributes/processing.rb:121:in `process_pending'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/attributes/processing.rb:28:in `process'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/document.rb:130:in `block in initialize'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/relations/builders.rb:47:in `building'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/document.rb:127:in `initialize'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:170:in `new'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:170:in `block in create'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:273:in `creating'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:169:in `create'
              from (irb):22
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.4/lib/rails/commands/console.rb:44:in `start'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.4/lib/rails/commands/console.rb:8:in `start'
              from /usr/local/rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.4/lib/rails/commands.rb:23:in `<top (required)>'
              from script/rails:6:in `require'
              from script/rails:6:in `<main>'
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            nickh Nick Hoffman
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: