Remove ActiveRecord in Rails 3

Ruby on-RailsRuby on-Rails-3

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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.

Solution 2 - Ruby on-Rails

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.

Solution 3 - Ruby on-Rails

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

Solution 4 - Ruby on-Rails

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.

Solution 5 - Ruby on-Rails

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.

Solution 6 - Ruby on-Rails

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

Solution 7 - Ruby on-Rails

Also comment out

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

(noted on rails 3.2.13)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark EmblingView Question on Stackoverflow
Solution 1 - Ruby on-RailsStéphan KochenView Answer on Stackoverflow
Solution 2 - Ruby on-RailsapeirosView Answer on Stackoverflow
Solution 3 - Ruby on-RailsJim GeurtsView Answer on Stackoverflow
Solution 4 - Ruby on-RailsvladView Answer on Stackoverflow
Solution 5 - Ruby on-RailsMichal KuklisView Answer on Stackoverflow
Solution 6 - Ruby on-RailsRimianView Answer on Stackoverflow
Solution 7 - Ruby on-RailsAndrew LankView Answer on Stackoverflow