Mongoid Identity Map设置未在Rails控制台中生效

时间:2013-05-13 22:36:46

标签: ruby-on-rails-3.2 mongoid3

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

只有我手动设置值才会生效:

{{1}}

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

使用Rails 3.2.13和Mongoid 3.1.2发生此问题。

1 个答案:

答案 0 :(得分: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的{​​{3}}。

 $ 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

对于在MongoDB数据库中设置的options,它们不能嵌套在session内。请参阅source的来源。