【问题标题】:Mongoid's strange behaviour in production: it says it saved the record, but it did not appear in dbMongoid在生产中的奇怪行为:它说它保存了记录,但它没有出现在db中
【发布时间】:2011-08-02 19:29:02
【问题描述】:

我有 3 个模型:品牌、模型和设备。品牌嵌入模型,模型嵌入设备。在 OSX 上的开发中一切正常,但在 EC2 Ubuntu 10.04 上的生产中,设备通过了所有验证,在创建和保存方法上显示为 true,甚至输出“设备已成功创建”,但不在 db 中。

ruby-1.9.2-p180 :002 > b = Brand.create!(:title => "Volvo", :logo_file_name => "some_logo.png")
=> #<Brand _id: 4da29833b177070722000002, _type: nil, _id: BSON::ObjectId('4da29833b177070722000002'), title: "Volvo", logo_file_name: "some_logo.png", logo_content_type: nil, logo_file_size: nil, logo_updated_at: nil, active: true>
ruby-1.9.2-p180 :007 > m = b.models.create!(:title => "740")
=> #<Model _id: 4da29893b177070722000004, _type: nil, _id: BSON::ObjectId('4da29893b177070722000004'), title: "740", active: true> 
ruby-1.9.2-p180 :024 > e = m.equipment.create!(:title => "1.6 Turbo", :start => 1987, :stop => 1993)
=> #<Equipment _id: 4da29a42b177070722000006, _type: nil, _id: BSON::ObjectId('4da29a42b177070722000006'), title: "1.6 Turbo", start: 1987, stop: 1993, acive: true, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil> 
ruby-1.9.2-p180 :025 > bb = Brand.last
=> #<Brand _id: 4da29833b177070722000002, _type: nil, _id: BSON::ObjectId('4da29833b177070722000002'), title: "Volvo", logo_file_name: "some_logo.png", logo_content_type: nil, logo_file_size: nil, logo_updated_at: nil, active: true> 
ruby-1.9.2-p180 :026 > mm = bb.models.last
=> #<Model _id: 4da29893b177070722000004, _type: nil, _id: BSON::ObjectId('4da29893b177070722000004'), title: "740", active: true> 
ruby-1.9.2-p180 :027 > ee = mm.equipment.last
=> nil

ruby-1.9.2-p180 :032 > mm.equipment
=> [] 

ruby-1.9.2-p180 :033 > e = m.equipment.new
=> #<Equipment _id: 4da29b15b177070722000008, _type: nil, _id: BSON::ObjectId('4da29b15b177070722000008'), title: nil, start: nil, stop: nil, acive: true, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil> 
ruby-1.9.2-p180 :034 > e.title = "1.8 SLE"
=> "1.8 SLE" 
ruby-1.9.2-p180 :035 > e.start = 1988
=> 1988 
ruby-1.9.2-p180 :036 > e.stop = 1994
=> 1994 
ruby-1.9.2-p180 :037 > e.save!
=> true 
ruby-1.9.2-p180 :038 > bb = Brand.last
=> #<Brand _id: 4da29833b177070722000002, _type: nil, _id: BSON::ObjectId('4da29833b177070722000002'), title: "Volvo", logo_file_name: "some_logo.png", logo_content_type: nil, logo_file_size: nil, logo_updated_at: nil, active: true> 
ruby-1.9.2-p180 :040 > mm = bb.models.last
=> #<Model _id: 4da29893b177070722000004, _type: nil, _id: BSON::ObjectId('4da29893b177070722000004'), title: "740", active: true> 
ruby-1.9.2-p180 :041 > ee = mm.equipment.last
=> nil 

为什么会发生这种情况?

【问题讨论】:

    标签: ruby-on-rails-3 ubuntu mongodb amazon-ec2 mongoid


    【解决方案1】:

    验证成功时返回True,不一定持久化到mongodb。

    默认情况下,大多数驱动程序(不确定 Mongoid)以“即发即弃”的方式写入 mongo,并且不要等待从服务器获得关于保存记录是否有问题的响应。

    您可以在 http://mongoid.org/docs/installation/configuration.html 默认打开“安全模式”,这将阻止驱动程序直到更改被持久化,而不是“触发和忘记”。

    也许您在服务器上遇到了一个您错过的错误,因为驱动程序/mongoid 只是没有等待响应(不安全的写入)。

    【讨论】:

    • 好吧,在控制台中一切都还是一样的(如果尝试创建另一个具有相同标题的标题,则创建甚至告诉标题已被采用,尽管我仍然无法在 db 中找到它),但是在网络界面中,我得到了新的非常奇怪的错误:Mongo::OperationFailure (: Modifier spec implies existence of an encapsulating object with a name that already represents a non-object, or is referenced in another $set clause): app/controllers/equipment_controller.rb:51:in create' `
    • 我配置了它。不知何故,我有旧版本的 mongod -- 1.2.2,所以我必须更新到 1.8.1,现在一切正常。谢谢!
    猜你喜欢
    • 2015-11-25
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    相关资源
    最近更新 更多