100

Now that Rails 3 beta is out, I thought I'd have a look at rewriting an app I have just started work on in Rails 3 beta, both to get a feel for it and get a bit of a head-start. The app uses MongoDB and MongoMapper for all of its models and therefore has no need for ActiveRecord. In the previous version, I am unloading activerecord in the following way:

config.frameworks -= [ :active_record ]    # inside environment.rb

In the latest version this does not work - it just throws an error:

/Library/Ruby/Gems/1.8/gems/railties-3.0.0.beta/lib/rails/configuration.rb:126:in
  `frameworks': config.frameworks in no longer supported. See the generated 
  config/boot.rb for steps on how to limit the frameworks that will be loaded 
  (RuntimeError)
 from *snip*

Of course, I have looked at the boot.rb as it suggested, but as far as I can see, there is no clue here as to how I might go about unloading AR. The reason I need to do this is because not only is it silly to be loading something I don't want, but it is complaining about its inability to make a DB connection even when I try to run a generator for a controller. This is because I've wiped database.yml and replaced it with connection details for MongoDB in order to use this gist for using database.yml for MongoDB connection details. Not sure why it needs to be able to initiate a DB connection at all just to generate a controller anyway....

Is anyone aware of the correct Rails 3 way of doing this?

7 Answers 7

155

I'm going by this from reading the source, so let me know if it actually worked. :)

The rails command that generates the application template now has an option -O, which tells it to skip ActiveRecord.

If you don't feel like rerunning rails, you should check the following in your existing app:

  • Check that your config/application.rb doesn't have require 'rails/all' or require "active_record/railtie". Instead, for a standard Rails setup without ActiveRecord, it should have only the following requires:

    require File.expand_path('../boot', __FILE__)
    
    require "action_controller/railtie"
    require "action_mailer/railtie"
    require "active_resource/railtie"
    require "rails/test_unit/railtie"
    require "sprockets/railtie"
    
    # Auto-require default libraries and those for the current Rails environment. 
    Bundler.require :default, Rails.env
    
  • If, in config/application.rb, you are using the config.generators section, make sure it doesn't have the line g.orm :active_record. You can set this explicitly to nil, if you want, but this should be the default when g.orm is completely omitted.

  • Optional, but in your Gemfile, remove the gem line that loads the module for your database. This could be the line gem "mysql" for example.

7
  • 3
    Yup, that seems to have done it. It appears the -O option can also be invoked as '--skip-activerecord'. I ran the rails command for another (temporary) app with this option and it generated a new app template matching exactly what you have written above. So I could just copy and paste these changes over to my actual app. Thanks - nice find :) Feb 6, 2010 at 11:36
  • 1
    Excellent - thanks for the --skip-activerecord. That's just what I needed.
    – Finglas
    Nov 9, 2010 at 13:34
  • That works for the most part, except when I run rails generate - I still see ActiveRecord stuff there - how do I remove it?
    – Hackeron
    Jan 11, 2011 at 19:52
  • 8
    In rails 3.1 you also need to have require "sprockets/railtie" in your application.rb file. Aug 15, 2011 at 14:41
  • 1
    Just to add a quick update to this answer and my previous comment... in Rails 3.1, it is now --skip-active-record. Notice the extra dash. Sep 3, 2011 at 13:56
46

Rails 4

I was looking for how to disable it in rails 4 and only found this answer which no longer works in rails 4. So this is how you can do it in rails 4 (tested in RC1).

In a new project

rails new YourProject --skip-active-record

In an existing project

  • In your Gemfile, remove the database driver gem, e.g. gem 'sqlite3' or gem 'pg'.
  • In config/application.rb, replace require 'rails/all' with

    require "action_controller/railtie"
    require "action_mailer/railtie"
    require "sprockets/railtie"
    require "rails/test_unit/railtie"
    

  • In config/environments/development.rb, remove or comment out config.active_record.migration_error = :page_load

  • Potentially you have to remove active_record helpers from the spec_helper (via VenoM in the comments)

  • Potentially you have to remove the ConnectionManagement middleware (seems to be the case with unicorn): config.app_middleware.delete "ActiveRecord::ConnectionAdapters::ConnectionManagement" (via https://stackoverflow.com/a/18087332/764342)

I hope this helps others looking for how to disable ActiveRecord in Rails 4.

5
  • 3
    Thanks! Still you need to remove active_record helpers from the spec_helper(Not mandatory, but if you are running specs). See last comment by Rimian.
    – VenoM
    Jul 8, 2013 at 12:01
  • Helped me, thanks! Per the comments below, there may be other config declarations to comment out in application.rb and/or environments/{env}.rb files. YMMV. Jul 26, 2013 at 19:56
  • the activerecord still remains in the gemfile.lock... Can we do something against it?
    – Boti
    Aug 14, 2013 at 9:07
  • @Boti Unlikely. It's part of rails. To remove it from your .lock I imagine you'd have to remove the dependency on rails and depend on all the individual components. Check out the gemspec for the list of dependencies -- you'd want to depend on everything but activerecord. github.com/rails/rails/blob/master/rails.gemspec Sep 4, 2013 at 16:58
  • I seem to be running into a i18n related problem after making this change: lib/active_support/i18n.rb:13:in `<top (required)>': uninitialized constant I18n (NameError). Any ideas? Aug 19, 2014 at 0:33
36

For a new rails app, you can have it exclude active record by specifying the --skip-active-record parameter. Eg:

rails new appname --skip-active-record
1
  • I did this and ran a few diffs to find and remove the stuff in my current app that was using ActiveRecord Apr 5, 2013 at 18:32
15

If you generated a new project using Rails 3.2, you will also need to comment out:

config.active_record.mass_assignment_sanitizer = :strict

and

config.active_record.auto_explain_threshold_in_seconds = 0.5

in your development.rb file.

6

All of the above are true. The one more thing which I had to do in rails 3.1 is to comment out

config.active_record.identity_map = true

in config/application.rb.

2

If you're running rspec, you also need to remove (in spec_helper):

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

and remove

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true
1

Also comment out

# config/application.rb    
config.active_record.whitelist_attributes = true

(noted on rails 3.2.13)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.