2

I made a new project and by default I got sqlite3 in my gemfile. I did some work in the project, but now I want to install the mongoid gem. I already have mongodb setup in my system.

I used this link to remove ActiveRecord. I removed sqlite3 and added 'gem mongoid' in gemfile. I ran bundle install. However, even though I have removed sqlite, ActiveRecord still persists in gemfile.lock.

How can I remove ActiveRecord and all other related dependencies from my gemfile.lock?

Update - This is a small project. What if someone have to switch from ActiveRecord to Mongoid in a big, live project? How could a person migrate in such a scenario?

Community
  • 1
  • 1
  • Delete your Gemfile.lock file and again run bundle install. This must help. If this doesn't help there is another possibility that any other gem you have installed requires activerecord gem. – Pradeep Sapkota Feb 15 '17 at 08:39
  • I cannot delete my gemfile in this (rails 5) project, even through terminal and I also checked rails itself have dependency on activerecord. – rahul kumar Feb 15 '17 at 09:57
  • What do you mean you can't remove it? `rm Gemfile.lock`? – Fredrik Feb 15 '17 at 10:09
  • I used the commoad but file regenerates itself. I even used 'rm -r Gemfile.lock'. I am able to delete the file in rails 4 project but not in rails 5 project. – rahul kumar Feb 15 '17 at 10:12
  • This file is generated only after you run bundle install. I'm also using rails 5 but I can delete the file. – Pradeep Sapkota Feb 16 '17 at 08:51

2 Answers2

1

You don't need to remove the activerecord gem to switch ORM's to Mongoid. Which is actually not possible since its a dependency of Rails.

To remove ActiveRecord from the application:

1. Get rid of the railtie.

Railties are how you load parts of the framework in rails.

# config/application.rb
require_relative 'boot'

require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
# require "active_record/railtie" -- remove or comment this line!
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"

This means that the active_record gem will no longer be required and loaded into memory.

2. Clean up the configuration

When you generate a new rails app your config has quite a few ActiveRecord specific options. So use grep or the search option in your favorite editor to remove any lines containing and remove all lines containing config.active_record from config/environments/*.yml.

3. Remove cruft

You can then get rid of the following files/folders:

  • /config/database.yml
  • /db/schema.rb
  • /db/migrate

Update - This is a small project what if someone have to switch from active record to mongoid in a live and big project. How will a person can migrate in such a scenario?

Switching ORMs in a mature project is pretty rare as it will require extensive rebuilding. But you would follow the steps above or run Mongoid and ActiveRecord in parallel until the conversion is complete. This is hardly something you would do live - it is most like a long running major version project.

max
  • 96,212
  • 14
  • 104
  • 165
0

This could be possible if you have another gem in Gemfile which have dependency on activerecord. In your case it's rails so you can just leave it here.

GEM
  remote: https://rubygems.org/
  specs:
    ...
    rails (4.2.5.1)
      actionmailer (= 4.2.5.1)
      actionpack (= 4.2.5.1)
      actionview (= 4.2.5.1)
      activejob (= 4.2.5.1)
      activemodel (= 4.2.5.1)
      activerecord (= 4.2.5.1)
droptheplot
  • 608
  • 6
  • 22