我有一个文档Portfolio,其中包含一系列嵌入式文档PortfolioItem portfolio_items。每个都有另一个图像图像阵列。不知怎的,我可以像这里更新数组中的第一个对象:
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"=>"✓", "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 to http://0.0.0.0:3000/admin/portfolios/5436dc1c646172844b000000/portfolio_items/5436dc27646172844b010000/images/5436dc40646172844b020000/edit
Completed 302 Found in 21ms
但如果我尝试更新另一个,使用等效输出,它与我想要更新的对象正确相关
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"=>"✓", "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 to http://0.0.0.0:3000/admin/portfolios/5436dc1c646172844b000000/portfolio_items/5436dc27646172844b010000/images/5436de41646172844b040000/edit
Completed 302 Found in 26ms
我实际上更新了同一个集合中的另一个对象(当然,还有一个不同的id),即同样是集合中的第一个对象。
控制器代码选择正确的对象,这似乎是创建这个混乱的更新。任何人都有一个想法,我能做些什么呢?
以下是我的控制器中发生的情况,但我无法检测到错误,始终选择了正确的对象:
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
此外,我没有收到任何错误消息。
这是一个控制台会话,非常清楚地显示行为
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)> i = 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 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>, #<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 was changed'
=> "i was 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)> i = 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 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>, #<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 was changed'}
=> [#<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>]
[14] pry(main)> changed = imgs.select {|x| x.title == 'i was changed'}
型号:
对于令人难以置信的重要信息 - 一项不会失败的测试:
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 << image1
item.images << image2
item.images << image3
p.portfolio_items << 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
答案 0 :(得分:1)
所以,我刚刚发现这是一个mongoDB错误,在这里咬我(为什么在webapp中而不是在测试中对我来说有点不清楚,但这是另一个故事)。问题是: https://jira.mongodb.org/browse/SERVER-831 以及将mongodb更新为2.6的结果。
答案 1 :(得分:0)
尝试
[6] pry(main)> img.save
<强> img.reload 强>
尝试在操作后从http://mongoid.org/en/mongoid/docs/querying.html:
重新加载模型对象label.bands.push(band)
label.bands #=> [ band ]
band.update_attribute(:active, false)
label.bands #=> [ band ] Must reload.
label.reload.bands #=> []