ruby on rails - mongoid does not update the right objects of an embedded collection -
ruby on rails - mongoid does not update the right objects of an embedded collection -
i have document portfolio array of embedded docs portfolioitem portfolio_items. each of has array of image images. somehow can update first object in array here:
started patch "/admin/portfolios/5436dc1c646172844b000000/portfolio_items/5436dc27646172844b010000/images/5436dc40646172844b020000" 127.0.0.1 @ 2014-10-14 17:06:13 -0300 processing admin::imagescontroller#update html parameters: {"utf8"=>"✓", "authenticity_token"=>"hhz3w90kmggk+lgtjlmgqmx295tvva6ificd4gqgg+8=", "image"=>{"title"=>"img 1", "description"=>"", "technique"=>"", "date"=>"", "public"=>"0"}, "commit"=>"atualizar image", "portfolio_id"=>"5436dc1c646172844b000000", "portfolio_item_id"=>"5436dc27646172844b010000", "id"=>"5436dc40646172844b020000"} moped: 127.0.0.1:27017 query database=portfolio_development collection=admins selector={"$query"=>{"_id"=>bson::objectid('4cb5fbc04174a17926000002')}, "$orderby"=>{:_id=>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"=>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"=>bson::objectid('5436dc1c646172844b000000'), "portfolio_items._id"=>bson::objectid('5436dc27646172844b010000'), "portfolio_items.0.images._id"=>bson::objectid('5436dc40646172844b020000')} update={"$set"=>{"portfolio_items.0.images.$.public"=>false}} flags=[] command database=portfolio_development command={:getlasterror=>1, :w=>1} runtime: 1.2640ms redirected http://0.0.0.0:3000/admin/portfolios/5436dc1c646172844b000000/portfolio_items/5436dc27646172844b010000/images/5436dc40646172844b020000/edit completed 302 found in 21ms
but if seek update one, equivalent output, relates correctly object want update
started patch "/admin/portfolios/5436dc1c646172844b000000/portfolio_items/5436dc27646172844b010000/images/5436de41646172844b040000" 127.0.0.1 @ 2014-10-14 17:03:04 -0300 processing admin::imagescontroller#update html parameters: {"utf8"=>"✓", "authenticity_token"=>"hhz3w90kmggk+lgtjlmgqmx295tvva6ificd4gqgg+8=", "image"=>{"title"=>"img 3", "description"=>"", "technique"=>"", "date"=>"", "public"=>"0"}, "commit"=>"atualizar image", "portfolio_id"=>"5436dc1c646172844b000000", "portfolio_item_id"=>"5436dc27646172844b010000", "id"=>"5436de41646172844b040000"} moped: 127.0.0.1:27017 command database=admin command={:ismaster=>1} runtime: 1.0710ms moped: 127.0.0.1:27017 query database=portfolio_development collection=admins selector={"$query"=>{"_id"=>bson::objectid('4cb5fbc04174a17926000002')}, "$orderby"=>{:_id=>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"=>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"=>bson::objectid('5436dc1c646172844b000000'), "portfolio_items._id"=>bson::objectid('5436dc27646172844b010000'), "portfolio_items.0.images._id"=>bson::objectid('5436de41646172844b040000')} update={"$set"=>{"portfolio_items.0.images.$.public"=>false}} flags=[] command database=portfolio_development command={:getlasterror=>1, :w=>1} runtime: 1.2790ms redirected http://0.0.0.0:3000/admin/portfolios/5436dc1c646172844b000000/portfolio_items/5436dc27646172844b010000/images/5436de41646172844b040000/edit completed 302 found in 26ms
i update object in same collection (with, of course, different id), namely, again, first object in collection.
the controller code selects right objects, update seems create mess. has idea, it?
here happens in controller, cannot observe error there, right objects selected:
class admin::imagescontroller < 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
also not getting error message.
here console session, shows behavior pretty clearly
loading development environment (rails 4.1.5) [1] pry(main)> p = portfolio.last => #<portfolio _id: 5436dc1c646172844b000000, index: 4, public: true, title: "test portfolio", _slugs: ["test-portfolio"], description: ""> [2] pry(main)> = p.portfolio_items.first => #<portfolioitem _id: 5436dc27646172844b010000, index: 0, public: true, title: "test item", _slugs: ["test-item"], client: "", description: "", date: nil> [3] pry(main)> imgs = i.images => [#<image _id: 5436dc40646172844b020000, index: 1, public: false, title: "i 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>, #<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>, #<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>, #<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>] [4] pry(main)> img = imgs[3] => #<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> [5] pry(main)> img.title = 'i changed' => "i changed" [6] pry(main)> img.save => true [7] pry(main)> img.reload => #<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> [8] pry(main)> [9] pry(main)> p = p.reload => #<portfolio _id: 5436dc1c646172844b000000, index: 4, public: true, title: "test portfolio", _slugs: ["test-portfolio"], description: ""> [10] pry(main)> = p.portfolio_items.first => #<portfolioitem _id: 5436dc27646172844b010000, index: 0, public: true, title: "test item", _slugs: ["test-item"], client: "", description: "", date: nil> [11] pry(main)> imgs = i.images => [#<image _id: 5436dc40646172844b020000, index: 1, public: false, title: "i 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>, #<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>, #<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>, #<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>] [12] pry(main)> img = imgs[3] => #<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> [13] pry(main)> changed = imgs.select {|x| x.title == 'i changed'} => [#<image _id: 5436dc40646172844b020000, index: 1, public: false, title: "i 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>] [14] pry(main)> changed = imgs.select {|x| x.title == 'i changed'}
models:
portfolio https://gist.github.com/janlimpens/81b70a45484f83765603 portfolioitem https://gist.github.com/janlimpens/bdfccb76e7db4ea7a8a9 image https://gist.github.com/janlimpens/478a3e9059032eae454band total mindboggling wtfing pleaseure - tests not fail:
test 'the right image updated' p = factorygirl.create(:portfolio) item = factorygirl.create(:portfolio_item) image1 = factorygirl.create(:image) image2 = factorygirl.create(:image) image3 = factorygirl.create(:image) item.images << image1 item.images << image2 item.images << image3 p.portfolio_items << item assert(p.valid?) p.save pos = 0..2 pos.each |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
so, found out mongodb bug bites me here (why in webapp , not in test bit unclear me, that's story). issue is: https://jira.mongodb.org/browse/server-831 , consequence updating mongodb 2.6.
ruby-on-rails mongodb mongoid
Comments
Post a Comment