MongoDB entity.save! 失败求解

carl · January 03, 2012 · Last by carl replied at February 04, 2012 · 2596 hits

使用 ruby on rails 开发,数据库用的是 mongodb, 其中一个 collection 中 entity 的结构如下:

{
  "name": "test"
  "password":"test"
    "method": [
    {
      "name":"test"
      "agus":"test"
      "owner":"test"
    }]
}

在 entity.rb 中使用 entity['name'] = 'ruby' entity['method'][0]['owner']= 'ruby' entity.save! 为什么只有 entity['name'] 正确保存了,而 entity['method'][0]['owner'] 不改变呢,有谁知道吗?谢谢!

enity 实体中设置了 accepts_nested_attributes_for 了吧

我记得很久以前 mongoid 是不支持

entity['method'][0]['owner']= 'ruby'  

这样的数组方式,而 ActiveRecord 支持,不知道现在是否支持

你试下修改成

entity['method'] = { 0 => {'owner'=> 'ruby' }  }

给你我以前调试时用的代码 https://github.com/saberma/shopqi/wiki/Mongoid 结合 mongoid 插件中的 lib/mongoid/nested_attributes.rb 一起调试

@saberma 解决了,谢谢

You need to Sign in before reply, if you don't have an account, please Sign up first.