Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In order to get mongo_mapper working in Rails (tested under Rails 3.1, should work in Rails 3.0.x as well), you have to follow these steps:</p> <ol> <li><p>In Gemfile, add the mongo_mapper and bson gem:</p> <pre><code>gem 'mongo_mapper' gem 'bson_ext' </code></pre></li> <li><p>Run bundle install</p></li> <li><p>In the config/ directory, create a mongo.yml file like this one:</p> <pre><code># config/mongo.yml defaults: &amp;defaults host: 127.0.0.1 port: 27017 development: &lt;&lt;: *defaults database: db_development username: user password: password test: &lt;&lt;: *defaults database: db_test # set these environment variables on your prod server production: &lt;&lt;: *defaults database: username: password: </code></pre></li> <li><p>In the config/initializers directory, create a mongo.rb file like this:</p> <pre><code> # config/initializers/mongo.rb #include MongoMapper db_config = YAML::load(File.read(File.join(Rails.root, "/config/mongo.yml"))) # if db_config[Rails.env] &amp;&amp; db_config[Rails.env]['adapter'] == 'mongodb' mongo = db_config[Rails.env] MongoMapper.connection = Mongo::Connection.new(mongo['host'] || 'localhost', mongo['port'] || 27017, :logger =&gt; Rails.logger) MongoMapper.database = mongo['database'] if mongo['username'] &amp;&amp; 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 </code></pre></li> <li><p>Start the mongod server before starting the rails server and enjoy!</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload