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?

mattmc3
  • 17,595
  • 7
  • 83
  • 103
Mark Embling
  • 12,605
  • 8
  • 39
  • 53

7 Answers7

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.

Tijmen
  • 149
  • 1
  • 6
Stéphan Kochen
  • 19,513
  • 9
  • 61
  • 50
  • 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 :) – Mark Embling Feb 06 '10 at 11:36
  • 1
    Excellent - thanks for the --skip-activerecord. That's just what I needed. – Finglas Nov 09 '10 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 '11 at 19:52
  • This trick causes the error "uninitialized constant Helper::Singleton" with Rails 3.0.5 – Dmytro Nesteriuk Mar 16 '11 at 16:25
  • 8
    In rails 3.1 you also need to have `require "sprockets/railtie"` in your `application.rb` file. – erskingardner Aug 15 '11 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. – Mark Embling Sep 03 '11 at 13:56
  • I found the rails/all in here: /var/lib/gems/1.9.1/gems/railties-3.2.13/lib/rails/all.rb This way you can find out what is required from version to version. – Daniel Jul 17 '13 at 00:06
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.

Community
  • 1
  • 1
apeiros
  • 1,695
  • 14
  • 11
  • 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 08 '13 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. – Nathan Smith Jul 26 '13 at 19:56
  • the activerecord still remains in the gemfile.lock... Can we do something against it? – Boti Aug 14 '13 at 09: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. https://github.com/rails/rails/blob/master/rails.gemspec – John Hinnegan Sep 04 '13 at 16:58
  • I seem to be running into a i18n related problem after making this change: lib/active_support/i18n.rb:13:in `': uninitialized constant I18n (NameError). Any ideas? – Michael Pell Aug 19 '14 at 00: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
Jim Geurts
  • 20,189
  • 23
  • 95
  • 116
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.

vlad
  • 497
  • 4
  • 6
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.

Michal Kuklis
  • 871
  • 11
  • 14
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
Rimian
  • 36,864
  • 16
  • 117
  • 117
1

Also comment out

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

(noted on rails 3.2.13)

Andrew Lank
  • 1,607
  • 1
  • 15
  • 29