Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mix mongodb and a traditional db in Rails?

Tags:

I am considering using MongoDB (mongo-mapper) for a portion of my rails application. I am not ready to go whole hog MongoDB because there are too many useful gems that depend on a traditional DB.

That being said there are parts of my application that would be great to leverage a document database.

Has anyone had success mixing the two approaches? How do you link activerecord models with mongomapper models?

like image 818
Jonathan Avatar asked Apr 21 '10 13:04

Jonathan


People also ask

Can you use MongoDB with Rails?

Mongoid 7.0. 5 or higher is required to use Rails 6.0. This generator will create the config/mongoid. yml configuration file, which is used to configure the connection to the MongoDB deployment.

Does Ruby on Rails support MongoDB?

The answer is yes: it makes a lot of sense. Nevertheless, Rails wasn't originally built to use a document database so you must use a separate gem in place of Active Record. MongoMapper and Mongoid are the two leading gems that make it possible use MongoDB as a datastore with Rails.

Why is MongoDB popular?

The sharding and aggregation framework, coupled with document validations, fine-grained locking, a mature ecosystem of tools and a vibrant community of users are some of the key reasons why MongoDB is the go-to database for many.

What is MongoDB in Rails?

“MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need.” In this article, I am going to show you how to setup schema-less database MongoDB with your Rails 5 application. I am using Ubuntu 14.04, Ruby 2.5. 1 and Rails 5.2 for this tutorial.


1 Answers

MongoMapper doesn't implement ActiveModel yet, but I think there are a few forks on github that do. You could use Mongoid instead (which does) and your relationships between Mongoid docs and ActiveRecord entries would just magically work. I know a number of people are doing that.

That said, I wouldn't want to mix them unless I absolutely had to have an RDBMS for some reason.

like image 104
MrKurt Avatar answered Oct 07 '22 01:10

MrKurt