Ask A Question

Notifications

You’re not receiving notifications from this thread.

Setting Up Rails 4 with MongoDB and Mongoid Discussion

Chris Oliver asked in General

If you have time could you please make a guide for OSX Users? :)

Reply

Only difference is you'll want to install Homebrew and then MongoDB through that using "brew install mongodb"

Reply
Wicky Andrian Wicky Andrian

great tuts, thanks for help

Reply

This is an area I've studied a lot lately, because I have an interest in using NoSQL instead of SQL databases for some of my models. Since we're on Rails 5 and Mongoid 6 now, I just thought I'd share these steps for setting it up now:

1) Create your Rails app with the '--skip-active-record' switch.

2) Remove sqlite3 from your Gemfile, add Mongoid to your Gemfile, and run 'bundle'.

3) Run 'rails g mongoid:config'.

4) Check your 'application.rb' file and make sure that inside the 'class Application' there is the line " Mongoid.load! './config/mongoid.yml' ". It's sometimes not included when the config is generated, but it's needed to use Mongoid.

5) Mongoid is ready to go. The Rails generators for 'model', 'scaffold' etc have been overridden by Mongoid. Any models, scaffolds etc that you create will create classes that include the Mongoid::Document module instead of inheriting from ApplicationRecord in the models folder.

-----

And something I've also found useful is using both ActiveRecord and Mongoid. MongoDB works great for some types of data, but sometimes I still feel more comfortable with SQL for some data, especially if I feel the need to wrap it in transactions.

1) Create your Rails app.

2) Add Mongoid to your Gemfile and run 'bundle'.

3) Run 'rails g mongoid:config'.

4) Check your 'application.rb' file and make sure that inside the 'class Application' there is the line " Mongoid.load! './config/mongoid.yml' ". It's sometimes not included when the config is generated, but it's needed to use Mongoid.

5) Mongoid is ready to go. The Rails generators for 'model', 'scaffold' etc have been overridden by Mongoid. Any models, scaffolds etc that you create will create
classes that include the Mongoid::Document module instead of inheriting
from ApplicationRecord in the models folder.

6) The ActiveRecord generators are still available, you just need to specify to use the 'active_record' versions as you use them. For example, at this point 'rails g model user email' generates a Mongoid model but 'rails g active_record:model user email' generates an ActiveRecord model and creates the needed migration. If you chose to make this a Mongoid model, there is no migration to worry about. If you chose to make this an ActiveRecord model, run the migration with 'rails db:migrate'. (Rails 5 moved rake functionality into the rails command)

7) You can create associations between ActiveRecord and Mongoid models by manually coding methods. You'll need to consider foreign keys. For example, if an ActiveRecord User has many Mongoid Posts, you'd add a foreign key field to the Post model. Its type would be Integer since ActiveRecord models use the Integer type for their id. Keep in mind that queries made from these associations can be slower because they traverse two data stores. Take things like lazy loading and the n+1 problem into consideration.

-----

An example application I created while tinkering with this is here. I created the ActiveRecord User and Mongoid Post example: https://github.com/welkie/e...

Reply

Thanks for sharing all this Matt! Will be useful for anyone reading this as I think it was originally written for Mongoid 4.

It's great to hear that they finally got the ActiveRecord + Mongoid stuff working nicely. I remember it wasn't super doable last time I used it because of some incompatibility.

Reply
Tiago Cassio Tiago Cassio

Thanks for sharing Matt!

Reply
FirstByte Technologies FirstByte Technologies

Thank you, @mattwelke:disqus, for the updates!

Reply
Maueez Ahmed Maueez Ahmed

@mattwelke:disqus Hi there,
I want to ask that how can we use firebase database with rails. Since it is nosql database as well. Are the steps same as you mentioned for mongodb. There is a gem on firebase-rails but i can't seem to find any proper documentation.

Thanks

Reply

Sorry about that, never realized you posted.

Rails is usually about relying on opinionated libraries that act as ActiveRecord adapters. We have that for SQL and MongoDB and actually a few others too (I know of Dynamoid for AWS DynamoDB). You could try finding one for Firebase. I know you say you found one that seemed to lack good documentation, so perhaps you've already exhausted this option.

If you can't find one, another good option which is a bit lower level but still convenient is to have your model class inherit from ActiveModel and then add the needed methods to satisfy the ActiveModel interface. You'd provide methods for getting it by id, saving, etc. This would mean using the official Ruby gem for Firebase, or if one isn't available, use a gem like HTTParty to interact with its REST API. Then you can use it in your controllers and views as you would any ActiveRecord model. Because Firebase is a document database, using it will be very similar to using Mongo.

However, if you go this route, you won't have associations handled for you so you'll have to make navigation methods between your Firebase ActiveModels like I made between ActiveRecord and Mongoid in my example repo.

Because this method is so flexible, and it would work with any persistence layer, you should be able to find example tutorials online for making your own ActiveModels.

Reply

Very nice, how would you go about connecting it to Mongo Atlas?

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,400+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.