使用Mongo Mapper的Rails

时间:2011-06-02 21:22:39

标签: ruby-on-rails mongodb

这是学习铁路的最糟糕的一天。已经无法连接和配置到sqlite3,mysql数据库我转向mongo mapper并面临同样的问题。当我尝试启动rails服务器时,我得到以下内容:

/usr/local/lib/ruby/gems/1.8/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require': no such file to load -- mongo_mapper (LoadError)

我已经安装了mongo_mapper gem但是当我执行'mongo_mapper'时它不会返回任何内容。

这是我的mongo_config.rb文件放在initializers文件夹中:

require "rubygems"
require "mongo_mapper"

MongoMapper.database = "blog-#{Rails.env}"

更新1:

我改为:

require "rubygems"
include MongoMapper

MongoMapper.database = "blog-#{Rails.env}"

现在我收到以下错误:

   /Users/azamsharp/Projects/railsprojects/blog/config/initializers/mongo_config.rb:2: uninitialized constant MongoMapper (NameError)

3 个答案:

答案 0 :(得分:4)

您必须将bundler与rails 3 一起使用。不要试图不使用它。将以下内容添加到您的Gemfile

source 'http://gemcutter.org'
source 'http://rubygems.org'

gem "rails", "3.0.7"
gem 'bson', "1.1.1"
gem 'bson_ext', "1.1.1"
gem 'mongo_mapper'

然后运行

bundle install

然后rails server将起作用

答案 1 :(得分:4)

为了让mongo_mapper在Rails中运行(在Rails 3.1下测试,也应该在Rails 3.0.x中运行),你必须遵循以下步骤:

  1. 在Gemfile中,添加mongo_mapper和bson gem:

    gem 'mongo_mapper'
    gem 'bson_ext'
    
  2. 运行捆绑安装

  3. 在config /目录中,创建一个像这样的mongo.yml文件:

    # config/mongo.yml
    
    defaults: &defaults
      host: 127.0.0.1
      port: 27017
    
    development:
      <<: *defaults
      database: db_development
      username: user
      password: password
    
    test:
      <<: *defaults
      database: db_test
    
    # set these environment variables on your prod server
    production:
      <<: *defaults
      database:
      username:
      password:
    
  4. 在config / initializers目录中,创建一个mongo.rb文件,如下所示:

     # config/initializers/mongo.rb
    
     #include MongoMapper
    
     db_config = YAML::load(File.read(File.join(Rails.root, "/config/mongo.yml")))
     #
     if db_config[Rails.env] && db_config[Rails.env]['adapter'] == 'mongodb'
       mongo = db_config[Rails.env]
       MongoMapper.connection = Mongo::Connection.new(mongo['host'] || 'localhost',
                                                  mongo['port'] || 27017,
                                                 :logger => Rails.logger)
       MongoMapper.database = mongo['database']
    
       if mongo['username'] && mongo['password']
         MongoMapper.database.authenticate(mongo['username'], mongo['password'])
       end
     end
    
     # Used for image uploads
     # CarrierWave.configure do |config|
     #   mongo = db_config[Rails.env]
     #   config.grid_fs_database = mongo['database']
     #   config.grid_fs_host = mongo['host'] || 'localhost'
     #   config.grid_fs_access_url = "gridfs"
     #   config.grid_fs_username = mongo['username']
     #   config.grid_fs_password =  mongo['password']
     # end
    
     # It's also possible to define indexes in the the model itself; however,
     # a few issues are being worked out still. This is a temporary solution.
     # Comment.ensure_index([["story_id", 1], ["path", 1], ["points", -1]])
     # MongoMapper.ensure_indexes!
    
     # Handle passenger forking.
     # if defined?(PhusionPassenger)
     #    PhusionPassenger.on_event(:starting_worker_process) do |forked|
     #      MongoMapper.database.connect_to_master if forked
     #    end
     # end
    
     if defined?(PhusionPassenger)
        PhusionPassenger.on_event(:starting_worker_process) do |forked|
          MongoMapper.connection.connect if forked
        end
     end
    
  5. 在启动rails服务器之前启动mongod服务器并享受!

答案 2 :(得分:-1)

尝试删除require行并将其替换为include MongoMapper