Use both MongoDB and MySQL database in rails project
You need to setup your rails project with both of the database connections.
Firstly for MongoDB, you need to decide which driver wrapper (if you use one) are you going to use. The two common ones are MongoID & MongoMapper. Both do pretty much the same thing and make it easy to interact with Mongo in a similar fashion to ActiveRecord.
To install these you merely install the appropriate gem e.g. for mongid you need to run ‘gem install mongoid’.
Add the following to the gemfile and run bundle install.
gem "mongoid", "~> 3.1.6"
Then run following command to generate the mongoid.yml file within config.
I wrote how to install and use MongoDB in my previous post.
Now for mysql, again there is a gem called ‘mysql2’. You can directly install mysql2 gem from rubygems.org.
Or add the following to the gemfile and run bundle instal.
I wrote setting mysql database in rails in my previous post.
Configured both database.yml and monogid.yml file within config file.
Configure Rails’ model generator. Inside of the Application class (config/application.rb) add:
config.generators do |g| g.orm :mongoid end
This will allow you to use the ‘rails generate model’ command with Mongoid.
Now if you want to generate the model with MySQL then use following command.
rails generate model User --orm=mysql2
Remember for both MongoDB and MySql you will need to have them installed and running somewhere that it accessible from development environment.