使用Rails 3.2.2安装Mongoid

时间:2012-03-16 19:07:55

标签: ruby-on-rails mongodb mongoid

我正在尝试使用带有rails 3.2.2的Mongoid,我添加了:

gem "mongoid", "~> 2.4"
gem "bson_ext", "~> 1.5"

到我的gemfile,运行:

bundle install

然后

rails g mongoid:config

然后编辑了我的application.rb所以:

#require "active_record/railtie" commented because of mongoid
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"

我有mongoid.yml:

development:
  host: localhost
  database: boards_development

test:
  host: localhost
  database: boards_test

# set these environment variables on your prod server
production:
  host: <%= ENV['MONGOID_HOST'] %>
  port: <%= ENV['MONGOID_PORT'] %>
  username: <%= ENV['MONGOID_USERNAME'] %>
  password: <%= ENV['MONGOID_PASSWORD'] %>
  database: <%= ENV['MONGOID_DATABASE'] %>
  # slaves:
  #   - host: slave1.local
  #     port: 27018
  #   - host: slave2.local
  #     port: 27019

我已经删除了databse.yml,就像在mongoid docs上说的那样但是在运行rails服务器时我得到的错误是这样的:

matteo@matteo-stable:~/Matteo/Dev/boards$ rails s
=> Booting WEBrick
=> Rails 3.2.2 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/railtie/configuration.rb:85:in `method_missing': undefined method `active_record' for #<Rails::Application::Configuration:0x8eb762c> (NoMethodError)
    from /home/matteo/Matteo/Dev/boards/config/environments/development.rb:26:in `block in <top (required)>'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/railtie/configurable.rb:24:in `class_eval'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/railtie/configurable.rb:24:in `configure'
    from /home/matteo/Matteo/Dev/boards/config/environments/development.rb:1:in `<top (required)>'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:251:in `require'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:251:in `block in require'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:251:in `require'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/engine.rb:571:in `block in <class:Engine>'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/initializable.rb:30:in `instance_exec'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/initializable.rb:30:in `run'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/initializable.rb:54:in `each'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/initializable.rb:54:in `run_initializers'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/application.rb:136:in `initialize!'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /home/matteo/Matteo/Dev/boards/config/environment.rb:5:in `<top (required)>'
    from /home/matteo/Matteo/Dev/boards/config.ru:4:in `require'
    from /home/matteo/Matteo/Dev/boards/config.ru:4:in `block in <main>'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/builder.rb:51:in `instance_eval'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/builder.rb:51:in `initialize'
    from /home/matteo/Matteo/Dev/boards/config.ru:1:in `new'
    from /home/matteo/Matteo/Dev/boards/config.ru:1:in `<main>'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/builder.rb:40:in `eval'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/builder.rb:40:in `parse_file'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/server.rb:200:in `app'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/commands/server.rb:46:in `app'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/server.rb:301:in `wrapped_app'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/server.rb:252:in `start'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/commands/server.rb:70:in `start'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/commands.rb:55:in `block in <top (required)>'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/commands.rb:50:in `tap'
    from /home/matteo/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/commands.rb:50:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
matteo@matteo-stable:~/Matteo/Dev/boards$ rails s
=> Booting WEBrick
=> Rails 3.2.2 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
host is not a valid option for Mongo::Connection
[2012-03-16 20:00:08] INFO  WEBrick 1.3.1
[2012-03-16 20:00:08] INFO  ruby 1.9.3 (2012-02-16) [i686-linux]
[2012-03-16 20:00:08] INFO  WEBrick::HTTPServer#start: pid=9936 port=3000
^C[2012-03-16 20:01:03] INFO  going to shutdown ...
[2012-03-16 20:01:03] INFO  WEBrick::HTTPServer#start done.
Exiting

离开关于活动记录的行我没有得到任何错误但是:

host is not a valid option for Mongo::Connection

我应该安装其他东西吗?

3 个答案:

答案 0 :(得分:6)

您从应用中删除了ActiveRecord,但您仍然拥有其配置选项。查看environment.rb和application.rb(以及/ config下的其他文件)中的“active_record”。

答案 1 :(得分:5)

下次尝试使用rails模板。它们将帮助您更快地开始,并将很快添加许多其他不错的宝石:

https://github.com/RailsApps/rails3-application-templates

如果你想设置一个没有活动记录的rails3项目,请使用此命令并稍后将mongoid添加到Gemfile而不会有任何麻烦:

rails new appname --skip-active-record

答案 2 :(得分:3)

您还必须对config / environments / development.rb

中的代码行进行注释

从Rails 3.2开始,他们在这个文件中添加了2行代码,这些代码调用了“active_record”。只需评论

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

  # Log the query plan for queries taking more than this (works
  # with SQLite, MySQL, and PostgreSQL)
  config.active_record.auto_explain_threshold_in_seconds = 0.5
编辑:关于主机问题,这很奇怪,但答案是不,你不应该安装别的东西......