MongoDB rails 3.2.1 使用 mongoid 提示错误

leeboqiang · 2012年02月17日 · 最后由 sanivbyfish 回复于 2012年08月28日 · 4760 次阅读
/Users/me/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/railtie/configuration.rb:85:in `method_missing': undefined method `active_record' for #<Rails::Application::Configuration:0x007fb1093b8140> (NoMethodError)
    from /Users/me/work/blog/config/environments/development.rb:26:in `block in <top (required)>'
    from /Users/me/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/railtie/configurable.rb:24:in `class_eval'
    from /Users/me/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/railtie/configurable.rb:24:in `configure'
    from /Users/me/work/blog/config/environments/development.rb:1:in `<top (required)>'
    from /Users/me/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:251:in `require'
    from /Users/me/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:251:in `block in require'
    from /Users/me/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /Users/me/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:251:in `require'
    from /Users/me/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/engine.rb:571:in `block in <class:Engine>'
    from /Users/me/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/me/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/initializable.rb:30:in `run'
    from /Users/me/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/me/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/initializable.rb:54:in `each'
    from /Users/me/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/me/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/application.rb:136:in `initialize!'
    from /Users/me/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /Users/me/work/blog/config/environment.rb:5:in `<top (required)>'
    from /Users/me/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/application.rb:103:in `require'
    from /Users/me/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/application.rb:103:in `require_environment!'
    from /Users/me/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/commands.rb:25:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

按照官方网站 安装好了 mongoid 修改了 application 配置文件 生成了 mongoid.yml 新建模型的时候报错 新建 scaffold 也报相同的错误 各位帮帮忙了

lz 新建项目的时候选择了-O(-skip-active-record) 么?

config/environments/development.rb

注释掉 26 行和 30 行

config.active_record.mass_assignment_sanitizer = :strict
config.active_record.auto_explain_threshold_in_seconds = 0.5

我记得照官网的来就可以了,不行的话,我这里刚好做了个基于 rails 3.2.1 + mongoid 的 demo,可以发给你。

记得 config/application.rb 中的这句要注释一下

# require "sprockets/railtie" # Uncomment this line for Rails 3.1+

#2 楼 @metal 用你的方法搞定了 ..为什么官网没有说呢 ..这是怎么回事啊?我记得老版本没有这个问题 3.1 我试过 谢谢你了

#4 楼 @hhuai 这个肯定改了啊 呵呵

#6 楼 @leeboqiang 恰好这两个是 3.2.1 新加的。

测了一下,并不需要改什么,在建工程时记得 skip activerecord 就行了。

记得还要去 config/environments/test.rb,注释下面这行

# Raise exception on mass assignment protection for Active Record models
# config.active_record.mass_assignment_sanitizer = :strict

否则测试的时候会报一样的错误

我和楼主一样的错误。但是在 application 没发现 sprockets/railtie 这个啊

需要 登录 后方可回复, 如果你还没有账号请 注册新账号