<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sanjay Prajapati</title>
    <description>The latest articles on Forem by Sanjay Prajapati (@prsanjay).</description>
    <link>https://forem.com/prsanjay</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F94618%2Fb7ca8981-0a79-4b28-a958-57eecbb9644a.png</url>
      <title>Forem: Sanjay Prajapati</title>
      <link>https://forem.com/prsanjay</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/prsanjay"/>
    <language>en</language>
    <item>
      <title>How to install and use MongoDB with Rails 6</title>
      <dc:creator>Sanjay Prajapati</dc:creator>
      <pubDate>Wed, 01 Apr 2020 13:12:38 +0000</pubDate>
      <link>https://forem.com/botreetechnologies/how-to-install-and-use-mongodb-with-rails-6-meg</link>
      <guid>https://forem.com/botreetechnologies/how-to-install-and-use-mongodb-with-rails-6-meg</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8NXoqAZL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.botreetechnologies.com/blog/wp-content/uploads/2020/04/how-to-install-and-use-mongodb-with-rails-6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8NXoqAZL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.botreetechnologies.com/blog/wp-content/uploads/2020/04/how-to-install-and-use-mongodb-with-rails-6.jpg" alt="How to install and use MongoDB with Rails 6"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Rails MongoDB : Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;MongoDB is a document database with the scalability, flexibility and the querying and indexing of the records.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this blog, we will cover how to install it in Ubuntu 16.04, configure and use it with &lt;a href="https://www.botreetechnologies.com/ruby-on-rails-development"&gt;Rails 6 application&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Install in Ubuntu 16.04 LTS
&lt;/h3&gt;

&lt;p&gt;The steps are for Ubuntu 16.04 but you can install in other versions as well.&lt;/p&gt;

&lt;p&gt;Follow below steps in your terminal to install it from the source.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After adding this repo update the packages&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo apt-get update&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, install mongo db using below command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get install -y mongodb-org

`If you face any issue like below`

WARNING: The following packages cannot be authenticated!

mongodb-org-shell mongodb-org-server mongodb-org-mongos mongodb-org-tools mongodb-org

E:There were unauthenticated packages and -y was used without --allow-unauthenticated
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then install it using the below command. Please note the risk of overriding the authentication warning here.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo apt-get --allow-unauthenticated install -y mongodb-org&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Configuration and Starting the Services
&lt;/h3&gt;

&lt;p&gt;After you are done with the installation, configure and start the service using below commands in your terminal&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo systemctl start mongod&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Check if the service has properly started or not using below command&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo systemctl status mongod&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using below command you can start it automatically when your system starts&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo systemctl enable mongod&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, MongoDB is set up and services are running. Let’s see how to use it with Rails 6&lt;/p&gt;

&lt;h3&gt;
  
  
  Read Also: &lt;a href="https://www.botreetechnologies.com/blog/notable-activerecord-changes-in-rails-6-part-3"&gt;Notable ActiveRecord Changes in Rails 6&lt;/a&gt;
&lt;/h3&gt;

&lt;h3&gt;
  
  
  3. Setup with Rails 6
&lt;/h3&gt;

&lt;p&gt;Create a new rails application using below command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails new mongorails --skip-active-record --skip-bundle&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We have used &lt;code&gt;--skip-active-record&lt;/code&gt; dependencies as we are not using it. We also used  &lt;code&gt;--skip-bundle&lt;/code&gt; to skip gem installations as we need to change the gemfile to add MongoDB dependency.&lt;/p&gt;

&lt;p&gt;If you are going to to use RSpec for testing then you can create your application using below command by skipping the default testing framework of rails&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails new mongorails --skip-active-record --skip-bundle --skip-test --skip-system-test&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, go to your project directory and add &lt;a href="https://github.com/mongodb/mongoid/"&gt;mongoid&lt;/a&gt; gem in Gemfile. Mongoid is the officially supported ODM (Object-Document-Mapper) framework for MongoDB in &lt;a href="https://www.botreetechnologies.com/blog/ruby-on-rails-development-obstacles-opportunities"&gt;Ruby on Rails Development&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;gem 'mongoid', '~&amp;gt; 7.0.5'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;NOTE:  Mongoid 7.0.5 or higher is required to use Rails 6.0.&lt;/p&gt;

&lt;p&gt;Run below command to install the gem&lt;/p&gt;

&lt;p&gt;&lt;code&gt;bundle install&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Run below command to generate a default configuration&lt;/p&gt;

&lt;p&gt;&lt;code&gt;bin/rails g mongoid:config&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command would create a &lt;code&gt;mongoid.yml&lt;/code&gt; file in &lt;code&gt;config&lt;/code&gt; directory which is a replacement of database.yml which rails create by default for us.&lt;/p&gt;

&lt;p&gt;It is recommended to change &lt;code&gt;server_selection_timeout&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;from 30 seconds to 1 second for the development environment. You can change it in &lt;code&gt;mongoid.yml&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, start rails server to check if everything is set up and configured correctly&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails s&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you see below screen after visiting &lt;code&gt;localhost:3000&lt;/code&gt;  in your browser then everything is good so far.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Sr-Kna3O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.botreetechnologies.com/blog/wp-content/uploads/2020/04/rails6-mongodb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Sr-Kna3O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.botreetechnologies.com/blog/wp-content/uploads/2020/04/rails6-mongodb.png" alt="rails6 mongodb"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4. How to use with Rails
&lt;/h3&gt;

&lt;p&gt;Now, let's create scaffolds using rails and check how the Model is working with MongoDB.&lt;/p&gt;

&lt;p&gt;We will create a small application where people can create posts and can comment on it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating Posts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a post using scaffolding. Yes, you can use scaffolding with MongoDB as well, and it would create all necessary files as it does for other databases.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails generate scaffold Post title:string body:text&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The important point to note is that we do not have a db folder and there is no need to run migrations.&lt;/p&gt;

&lt;p&gt;Post model will look like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--19r5KXti--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.botreetechnologies.com/blog/wp-content/uploads/2020/04/post-model.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--19r5KXti--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.botreetechnologies.com/blog/wp-content/uploads/2020/04/post-model.png" alt="post model in mongodb ruby on rails"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, head to the browser and visit &lt;code&gt;localhost:3000/posts&lt;/code&gt; and you will see a list of posts and you can create/update/delete the posts. Same old rails stuff :)&lt;/p&gt;

&lt;p&gt;Once you create the post it will fire insert query as below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--024gImIl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.botreetechnologies.com/blog/wp-content/uploads/2020/04/insert-query.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--024gImIl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.botreetechnologies.com/blog/wp-content/uploads/2020/04/insert-query.png" alt="rails mongodb integration - insert query"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can notice that it has first found the database name &lt;code&gt;mongorails_development.insert&lt;/code&gt; and then used the below syntax to insert the record to the &lt;code&gt;posts&lt;/code&gt; table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{"insert"=&amp;gt;"posts", "documents"=&amp;gt;[{"_id"=&amp;gt;BSON::ObjectId('5e7d91f2d3527338a88a0751'), "title"=&amp;gt;"Shiny Post", "body"=&amp;gt;"Lorem ipsum"}], "ordered"=&amp;gt;true}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And when you visit show path of the app, it would fire below query&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mongorails_development.find | STARTED | {"find"=&amp;gt;"posts", "filter"=&amp;gt;{"_id"=&amp;gt;BSON::ObjectId('5e7d91f2d3527338a88a0751')}}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can find the post from your rails console using below command&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Post.find('5e7d91f2d3527338a88a0751')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating a comment&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s create comments on the blogs&lt;/p&gt;

&lt;p&gt;Run below command to create comment and associate with posts using scaffold&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails generate scaffold Comment name:string message:string post:belongs_to&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;By default, it would generate comment model code as below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h1Muy_2O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.botreetechnologies.com/blog/wp-content/uploads/2020/04/generate-comment-model-code.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h1Muy_2O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.botreetechnologies.com/blog/wp-content/uploads/2020/04/generate-comment-model-code.png" alt="ruby on rails with mongodb"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Change &lt;code&gt;embedded_in :post&lt;/code&gt; to &lt;code&gt;belongs_to :post&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Open &lt;code&gt;comments/_form&lt;/code&gt; and change the type of field for &lt;code&gt;:message&lt;/code&gt; from &lt;code&gt;text_field to text_area&lt;/code&gt;, as well as the type of field for &lt;code&gt;:post_id&lt;/code&gt; from text_field to hidden_field&lt;/p&gt;

&lt;p&gt;Add below code in &lt;code&gt;posts/show.html.erb&lt;/code&gt; to create comments for the post. This will list all the comments for the post and also allow you to add new comments.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jpAtB_DM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.botreetechnologies.com/blog/wp-content/uploads/2020/04/add-new-comments.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jpAtB_DM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.botreetechnologies.com/blog/wp-content/uploads/2020/04/add-new-comments.png" alt="ruby on rails mongodb"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, create &lt;code&gt;_comment.html.erb partial&lt;/code&gt; in &lt;code&gt;app/views/comments folder with the code below&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MX0HXNWJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.botreetechnologies.com/blog/wp-content/uploads/2020/04/folder-with-the-code-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MX0HXNWJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.botreetechnologies.com/blog/wp-content/uploads/2020/04/folder-with-the-code-1.png" alt="mongodb rails"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to add a new field to the table just add it using the below line. I have added a new field to posts table using below line&lt;/p&gt;

&lt;p&gt;&lt;code&gt;field :slug, type: String&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It is that simple!!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You don’t need to declare a foreign key field to comment on the model. It would implicitly create the post_id to comments table. The naming convention for a foreign key is the same as active records.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;That's it, You have created a small application using MongoDB.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>rails</category>
      <category>mongoid</category>
      <category>mongodb</category>
      <category>ruby</category>
    </item>
  </channel>
</rss>
