title: mongoid does not update the right objects of an embedded collection
body: <p>I have a document Portfolio with an array of embedded docs PortfolioItem portfolio_items. Each of those has another array of Image images. Somehow I can update the first object in the array like here:</p>
  
  <pre><code>Started PATCH "/admin/portfolios/5436dc1c646172844b000000/portfolio_items/5436dc27646172844b010000/images/5436dc40646172844b020000" for 127.0.0.1 at 2014-10-14 17:06:13 -0300
  Processing by Admin::ImagesController#update as HTML
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"hHz3W90KMgGK+LGtjLMGQmx295tvva6IFIcD4gqgG+8=", "image"=&gt;{"title"=&gt;"img 1", "description"=&gt;"", "technique"=&gt;"", "date"=&gt;"", "public"=&gt;"0"}, "commit"=&gt;"Atualizar Image", "portfolio_id"=&gt;"5436dc1c646172844b000000", "portfolio_item_id"=&gt;"5436dc27646172844b010000", "id"=&gt;"5436dc40646172844b020000"}
    MOPED: 127.0.0.1:27017 QUERY        database=portfolio_development collection=admins selector={"$query"=&gt;{"_id"=&gt;BSON::ObjectId('4cb5fbc04174a17926000002')}, "$orderby"=&gt;{:_id=&gt;1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 1.2910ms
    MOPED: 127.0.0.1:27017 QUERY        database=portfolio_development collection=portfolios selector={"_id"=&gt;BSON::ObjectId('5436dc1c646172844b000000')} flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 2.6960ms
    MOPED: 127.0.0.1:27017 UPDATE       database=portfolio_development collection=portfolios selector={"_id"=&gt;BSON::ObjectId('5436dc1c646172844b000000'), "portfolio_items._id"=&gt;BSON::ObjectId('5436dc27646172844b010000'), "portfolio_items.0.images._id"=&gt;BSON::ObjectId('5436dc40646172844b020000')} update={"$set"=&gt;{"portfolio_items.0.images.$.public"=&gt;false}} flags=[]
                           COMMAND      database=portfolio_development command={:getlasterror=&gt;1, :w=&gt;1} runtime: 1.2640ms
  Redirected to http://0.0.0.0:3000/admin/portfolios/5436dc1c646172844b000000/portfolio_items/5436dc27646172844b010000/images/5436dc40646172844b020000/edit
  Completed 302 Found in 21ms
  </code></pre>
  
  <p>but if I try to update another one, with the equivalent output, which relates correctly to the object I want to update</p>
  
  <pre><code>Started PATCH "/admin/portfolios/5436dc1c646172844b000000/portfolio_items/5436dc27646172844b010000/images/5436de41646172844b040000" for 127.0.0.1 at 2014-10-14 17:03:04 -0300
  Processing by Admin::ImagesController#update as HTML
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"hHz3W90KMgGK+LGtjLMGQmx295tvva6IFIcD4gqgG+8=", "image"=&gt;{"title"=&gt;"img 3", "description"=&gt;"", "technique"=&gt;"", "date"=&gt;"", "public"=&gt;"0"}, "commit"=&gt;"Atualizar Image", "portfolio_id"=&gt;"5436dc1c646172844b000000", "portfolio_item_id"=&gt;"5436dc27646172844b010000", "id"=&gt;"5436de41646172844b040000"}
    MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=&gt;1} runtime: 1.0710ms
    MOPED: 127.0.0.1:27017 QUERY        database=portfolio_development collection=admins selector={"$query"=&gt;{"_id"=&gt;BSON::ObjectId('4cb5fbc04174a17926000002')}, "$orderby"=&gt;{:_id=&gt;1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 1.0460ms
    MOPED: 127.0.0.1:27017 QUERY        database=portfolio_development collection=portfolios selector={"_id"=&gt;BSON::ObjectId('5436dc1c646172844b000000')} flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 2.8700ms
    MOPED: 127.0.0.1:27017 UPDATE       database=portfolio_development collection=portfolios selector={"_id"=&gt;BSON::ObjectId('5436dc1c646172844b000000'), "portfolio_items._id"=&gt;BSON::ObjectId('5436dc27646172844b010000'), "portfolio_items.0.images._id"=&gt;BSON::ObjectId('5436de41646172844b040000')} update={"$set"=&gt;{"portfolio_items.0.images.$.public"=&gt;false}} flags=[]
                           COMMAND      database=portfolio_development command={:getlasterror=&gt;1, :w=&gt;1} runtime: 1.2790ms
  Redirected to http://0.0.0.0:3000/admin/portfolios/5436dc1c646172844b000000/portfolio_items/5436dc27646172844b010000/images/5436de41646172844b040000/edit
  Completed 302 Found in 26ms
  </code></pre>
  
  <p>I actually update another object in the same collection (with, of course, a different id), namely, again, the first object in the collection.</p>
  
  <p>The controller code selects the right objects, it is the update that seems to create this mess. Anyone has an idea, what I could do about it?</p>
  
  <p>Here is what happens in my controller, but I cannot detect an error there, the correct objects were always selected:</p>
  
  <pre><code>class Admin::ImagesController &lt; Admin::AdminController
  
    before_filter :find_parents
  
    def find_parents
      @portfolio = Portfolio.find(params[:portfolio_id])
      @portfolio_item = @portfolio.portfolio_items.find(params[:portfolio_item_id])
      if params[:id]
        @image = @portfolio_item.images.find(params[:id])
      end
    end
  
    def update
      @image.update_attributes(image_params)
      if @image.valid?
        redirect_to :back
      else
        flash[:error] = t('simple_form.error_notification.default_message')
        render template: 'admin/images/edit'
      end
    end
  
   def image_params
      params.require(:image).permit(:title, :description, :technique, :date, :image, :public, :delete_image)
   end
  
  end
  </code></pre>
  
  <p>Also I not getting any error message.</p>
  
  <p>Here is a console session, that shows the behavior pretty clearly</p>
  
  <pre><code>Loading development environment (Rails 4.1.5)
  [1] pry(main)&gt; p = Portfolio.last
  =&gt; #&lt;Portfolio _id: 5436dc1c646172844b000000, index: 4, public: true, title: "test portfolio", _slugs: ["test-portfolio"], description: ""&gt;
  [2] pry(main)&gt; i = p.portfolio_items.first
  =&gt; #&lt;PortfolioItem _id: 5436dc27646172844b010000, index: 0, public: true, title: "test item", _slugs: ["test-item"], client: "", description: "", date: nil&gt;
  [3] pry(main)&gt; imgs = i.images
  =&gt; [#&lt;Image _id: 5436dc40646172844b020000, index: 1, public: false, title: "i was changed", _slugs: ["img-4", "i-was-changed-1"], description: "", technique: "", date: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, image_fingerprint: nil&gt;, #&lt;Image _id: 5436dc4b646172844b030000, index: 0, public: false, title: "img 2", _slugs: ["img-2"], description: "", technique: "", date: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, image_fingerprint: nil&gt;, #&lt;Image _id: 5436de41646172844b040000, index: 2, public: true, title: "img 3", _slugs: ["img-3"], description: "", technique: "", date: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, image_fingerprint: nil&gt;, #&lt;Image _id: 543d7ada6461722ec0010000, index: 3, public: false, title: "img 4", _slugs: ["img-4"], description: "", technique: "", date: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, image_fingerprint: nil&gt;]
  [4] pry(main)&gt; img = imgs[3]
  =&gt; #&lt;Image _id: 543d7ada6461722ec0010000, index: 3, public: false, title: "img 4", _slugs: ["img-4"], description: "", technique: "", date: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, image_fingerprint: nil&gt;
  [5] pry(main)&gt; img.title = 'i was changed'
  =&gt; "i was changed"
  [6] pry(main)&gt; img.save
  =&gt; true
  [7] pry(main)&gt; img.reload
  =&gt; #&lt;Image _id: 543d7ada6461722ec0010000, index: 3, public: false, title: "img 4", _slugs: ["img-4"], description: "", technique: "", date: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, image_fingerprint: nil&gt;
  [8] pry(main)&gt; 
  [9] pry(main)&gt; p = p.reload
  =&gt; #&lt;Portfolio _id: 5436dc1c646172844b000000, index: 4, public: true, title: "test portfolio", _slugs: ["test-portfolio"], description: ""&gt;
  [10] pry(main)&gt; i = p.portfolio_items.first
  =&gt; #&lt;PortfolioItem _id: 5436dc27646172844b010000, index: 0, public: true, title: "test item", _slugs: ["test-item"], client: "", description: "", date: nil&gt;
  [11] pry(main)&gt; imgs = i.images
  =&gt; [#&lt;Image _id: 5436dc40646172844b020000, index: 1, public: false, title: "i was changed", _slugs: ["img-4", "i-was-changed"], description: "", technique: "", date: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, image_fingerprint: nil&gt;, #&lt;Image _id: 5436dc4b646172844b030000, index: 0, public: false, title: "img 2", _slugs: ["img-2"], description: "", technique: "", date: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, image_fingerprint: nil&gt;, #&lt;Image _id: 5436de41646172844b040000, index: 2, public: true, title: "img 3", _slugs: ["img-3"], description: "", technique: "", date: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, image_fingerprint: nil&gt;, #&lt;Image _id: 543d7ada6461722ec0010000, index: 3, public: false, title: "img 4", _slugs: ["img-4"], description: "", technique: "", date: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, image_fingerprint: nil&gt;]
  [12] pry(main)&gt; img = imgs[3]
  =&gt; #&lt;Image _id: 543d7ada6461722ec0010000, index: 3, public: false, title: "img 4", _slugs: ["img-4"], description: "", technique: "", date: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, image_fingerprint: nil&gt;
  [13] pry(main)&gt; changed = imgs.select {|x| x.title == 'i was changed'}
  =&gt; [#&lt;Image _id: 5436dc40646172844b020000, index: 1, public: false, title: "i was changed", _slugs: ["img-4", "i-was-changed"], description: "", technique: "", date: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, image_fingerprint: nil&gt;]
  [14] pry(main)&gt; changed = imgs.select {|x| x.title == 'i was changed'}
  </code></pre>
  
  <p>Models:</p>
  
  <ul>
  <li>Portfolio <a href="https://gist.github.com/janlimpens/81b70a45484f83765603" rel="nofollow">https://gist.github.com/janlimpens/81b70a45484f83765603</a></li>
  <li>PortfolioItem <a href="https://gist.github.com/janlimpens/bdfccb76e7db4ea7a8a9" rel="nofollow">https://gist.github.com/janlimpens/bdfccb76e7db4ea7a8a9</a></li>
  <li>Image <a href="https://gist.github.com/janlimpens/478a3e9059032eae454b" rel="nofollow">https://gist.github.com/janlimpens/478a3e9059032eae454b</a></li>
  </ul>
  
  <p>And for total mindboggling wtfing pleaseure - a tests that does not fail:</p>
  
  <pre><code>test 'the correct image is updated' do
        p = FactoryGirl.create(:portfolio)
        item = FactoryGirl.create(:portfolio_item)
        image1 = FactoryGirl.create(:image)
        image2 = FactoryGirl.create(:image)
        image3 = FactoryGirl.create(:image)
        item.images &lt;&lt; image1
        item.images &lt;&lt; image2
        item.images &lt;&lt; image3
        p.portfolio_items &lt;&lt; item
        assert(p.valid?)
        p.save
        pos = 0..2
  
        pos.each do |i|
          title =  "Hello World #{i}"
          pt= Portfolio.first
          pt.portfolio_items[0].images[i].title = title
          pt.save
          pt.reload
  
          assert(pt.portfolio_items[0].images[i].title == title)
          pp pt.portfolio_items[0].images.map { |x| x.title }
        end
  end
  </code></pre>