How to configure MongoMapper and ActiveRecord in one Ruby Rails project

I have an existing Ruby / Rails application that I want to migrate to MongoDB over time, because time allows, because it is not an option to just rewrite everything all at once. I would like for me to simply condemn my old classes when I got to them. I plan to use MongoMapper. I cannot find an example where someone explains how to configure database configuration files to allow connection to both data stores in one application.

FWIW, I use Rails 3. I appreciate the help.

+7
source share
2 answers

Include your mongo_mapper stone in your Gemfile. Then in the models that you slowly want to port to MongoMapper, you simply include this in your model:

include MongoMapper::Document 

here is an example of a Mongo publishing model

 class Publisher include MongoMapper::Document key :_id, String key :mtd_uniques, Integer key :mtd_demo_uniques, Integer key :archive, Array key :ignore, Boolean end 

My user model (postgres):

 class User < ActiveRecord::Base validates_presence_of :first_name, :last_name, :email, :type acts_as_authentic def self.inherited(child) child.instance_eval do def model_name User.model_name end end super end end 

The best part is that all of your other models still use ActiveRecord, so you can use two different databases until everything is ported to Mongo. This is an example of what I am using. Big data aggregations using MongoMapper and User model using postgres (application hosted on Heroku)

For my setup, I dumped the configuration files into my config.yml

 development: adapter: MongoDB host: localhost database: my-dev-db test: adapter: MongoDB host: localhost database: my-dev-db staging: adapter: MongoDB host: remote-host (for me amazon ec2) database: my-staging-db production: adapter: MongoDB host: remote-host (for me amazon ec2) database: my-production-db 

and created an initializer that distinguishes 2 databases:

/initializers/database.rb

 # load YAML and connect database_yaml = YAML::load(File.read("#{Rails.root}/config/config.yml")) puts "Initializing mongodb" if database_yaml[Rails.env] && database_yaml[Rails.env]['adapter'] == 'MongoDB' mongo_database = database_yaml[Rails.env] MongoMapper.connection = Mongo::Connection.new(mongo_database['host'], 27017, :pool_size => 5, :timeout => 5) MongoMapper.database = mongo_database['database'] end 
+6
source

It looks like the initializer code from the previous answer is no longer needed. mongo_mapper by itself searches for the config/mongo.yml and analyzes it and starts the connection. Like ActiveRecord database.yml or Mongoid monogoid.yml .

Mongo mapper even comes with a Rails generator to install this file for you. All you have to do is:

 rails g mongo_mapper:config 

This led to:

 defaults: &defaults host: 127.0.0.1 port: 27017 development: <<: *defaults database: db_name_development ... 

Of course, it would be nice if they mentioned it in the Readme or on the doc website. I am using mongo_mapper version 0.10.1

+2
source

All Articles