How to setup Ruby on Rails App with MongoDB

27 de octubre, 2017 @ 12:52 — 0 comentarios

This tutorial has been created having in mind, it's going to be used with guebs.eu services, but may be also valid for general purpose.

1 - Create a MongoDB, database, user and password.

First thing first; you need some database and credentials to connect to.

Access your hosting panel at guebs.eu and create a new MongoDB in Database section.

2 - Create Ruby app

Second, you need to register a new Ruby App on guebs.eu hosting panel. You can set up this in the Application section in guebs.eu's hosting panel.

3 - Access your hosting via SSH

How to access your hosting via SSH

4 - Install Rails with MongoDB in mind

cd ruby

Should we conect to connect our rails connection to MongoDB, Active Records may be disabled.

rails new YOUR-APP-NAME --skip-active-record

We will have no database.yml file, and as we will be using the mongoid Gem, you need to add this line to your Gemfile:

gem 'mongoid'

As at today (26-6-2017 ) there is no need to especify a gem version, as the las version 6.2.0 is compatible with Rails 5.1 and Ruby 2.4

To activate NodeJS on our Ruby APP the following guide's 4th section should be used:

How to create a Rails 5 application from zero

You now need to create Mongoid config. Execute this command:

rails g mongoid:config

And set up accordingly config/mongoid.yml file:

production:
clients:
default:
database: railsmongo
hosts:
- 1.2.3.4
options:
read:
mode: :primary
max_pool_size: 1
user: railsmongo
password: password1

At this point, your App should be ready to connect to MongoDB.