Shorif Mahmud
Shorif Mahmud

Reputation: 11

In mongodb,How to use ruby on rails "migration" tools

In ruby on rails normally use SQLite as a database.so a special property of rails called migration are work.but when we use "mongodb" as a database in rails.I see there have no migrate folder in the db directory. Is there any way to use this migration property in rails when use mongodb.

Upvotes: 1

Views: 1920

Answers (1)

user3402754
user3402754

Reputation: 3085

According to the documentation here, db:migrate: Exists only for dependency purposes, but does not actually do anything. However, because I am not sure of what version of rails you're using, how your project was setup and if you intend using just mongodb I will describe the process for both possibilities from scratch with all assumptions if any clearly stated.

This approach assumes you want to use mongodb alone

  1. Create your Rails app with the --skip-active-record switch.
  2. Remove sqlite3 from your Gemfile
  3. add gem 'mongoid' to your Gemfile
  4. and run bundle
  5. Run rails g mongoid:config
  6. Check your application.rb file and make sure that inside the 'class Application' you have this line Mongoid.load! './config/mongoid.yml' It's sometimes not included when the config is generated, but is needed to use Mongoid.
  7. 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.

For instance, when you run

rails g model person first_name last_name email_address

if you open up the file app/models/person.rb

You'd see

class Person
  include Mongoid::Document
  field :first_name, type: String
  field :last_name, type: String
  field :email_address, type: String
end

Upvotes: 1

Related Questions