1

似乎没有从文件identity_map中获取设置。config/mongoid.yml

这是文件:

development:
  sessions:
    default:
      uri: mongodb://localhost:27017/test_development
      options: &defaultopts
        op_timeout: 60
        allow_dynamic_fields: false
        identity_map_enabled: true
        preload_models: true
        raise_not_found_error: false

运行时RAILS_ENV=development rails console,地图未打开:

$ RAILS_ENV=development rails c
Loading development environment (Rails 3.2.13)
[1] pry(main)> Mongoid.using_identity_map?
=> false
[2] pry(main)> Mongoid.identity_map_enabled?
=> false

即使尝试手动加载Mongoid并且文件也不会更改它:

[3] pry(main)> require 'mongoid'
=> false
[4] pry(main)> Mongoid.load!("./config/mongoid.yml")
=> {"sessions"=>
  {"default"=>
    {"uri"=>"mongodb://localhost:27017/test_development",
     "options"=>
      {"op_timeout"=>60,
       "allow_dynamic_fields"=>false,
       "identity_map_enabled"=>true,
       "preload_models"=>true,
       "raise_not_found_error"=>false}}}}
[5] pry(main)> Mongoid.using_identity_map?
=> false
[6] pry(main)> Mongoid.identity_map_enabled?
=> false

只有当我手动设置该值时,它才会生效:

[8] pry(main)> Mongoid.identity_map_enabled = true
=> true
[9] pry(main)> Mongoid.using_identity_map?                                                                                                                                                                                                    
=> true
[10] pry(main)> Mongoid.identity_map_enabled?
=> true

为什么设置没有正确加载?

使用 Rails 3.2.13 和 Mongoid 3.1.2 会出现此问题。

4

1 回答 1

1

:options不应该嵌套在:default. Mongoid 期望看到mongoid.yml以下格式:

development:
  sessions:
    default:
      uri: mongodb://localhost:27017/test_development
  options:
    op_timeout: 60
    allow_dynamic_fields: false
    identity_map_enabled: true
    preload_models: true
    raise_not_found_error: false

查看正在加载的源:options

 $ pry
 [1] pry(main)> require 'mongoid'
 => true
 [2] pry(main)> Mongoid.load!("./mongoid.yml", :production)
 => {"sessions"=>
    {"default"=>{"database"=>"mongoid_prod", "hosts"=>["localhost:27017"]}},
    "options"=>{"identity_map_enabled"=>true, "include_root_in_json"=>true}}
 [3] pry(main)> Mongoid.using_identity_map?
 => true
 [4] pry(main)>

编辑:正如@cbmanica 所指出的,options可以设置多个位置。例如我们可以有:

development:
  sessions:
    default:
      uri: mongodb://localhost:27017/test_development
      options:
        consistency: :strong
  options:
    op_timeout: 60

对于在optionsMongoDB 数据库上设置的那些,它们不能嵌套在session. 有关默认值,请参见源代码。

于 2013-05-14T04:58:02.330 回答