Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove ActiveRecord from Rails 3 application

I have read previous threads on this topic, but the solutions I've found there have not solved my problem. I have a rails 3.2.9 application that does not do database access - it uses an HTTP Rest protocol to another application to access persistent data. The app runs OK in a local test environment with "rails server" using WEbrick, but fails to run with Nginx/Passenger with the error "* Exception LoadError in application (Please install the sqlite3 adapter: gem install activerecord-sqlite3-adapter (sqlite3 is not part of the bundle. Add it to Gemfile.))". From the stack trace it would appear that ActiveRecord wants to eagerly establish a database connection in code that executes prior to the request being processed. I have tried to follow the instructions to remove ActiveRecord from my dependencies with no luck. I generated with --skip-activerecord, which produced an application.rb like this as expected:

require File.expand_path('../boot', __FILE__)

# Pick the frameworks you want:
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"

There are no references to activerecord or active_record anywhere in my application except in the gemlock file created by bundler, in comments and in readme. Bundler reports ActiveRecord as a dependency, and 'bundle viz' reports rails itself as being the gem requiring ActiveRecord. Any suggestions or advice would be most welcome.

In response to eric's question, here is my Gemfile

source 'https://rubygems.org'

gem 'rails', '3.2.9'

group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'
like image 466
Martin Nally Avatar asked Nov 04 '22 08:11

Martin Nally


1 Answers

As you have discovered Rails itself has ActiveRecord listed as a dependency in it's Gemspec. The setup you have archived so far is the standard way of removeing ActiveRecord from Rails. If you really want to go further and also remove the gem you will, most likely, have to fork the Rails gem and remove the dependency in it's Gemspec.

It might be that simple, but you might also find that there is additional glue code in Rails to tie ActiveRecord in and you will have to remove that as well. All in all I'm wondering if its worth it.

If you don't need ActiveRecord you have already prevented it from effectively loading. Some parts might still load, but most of it wont. The win in memory footprint/performance against the time you will spend removing a core Rails feature makes me wonder if you are not looking at the wrong framework for your needs?

If you have requirements that are so tight that Rails is still to heavy you should probably look at Sinatra or similar instead. You could also stick with Rails and do a custom Rack middleware stack to keep just the parts of the call stack that you need.

I hope this gave you some guidance to, if not a viably solution, some alternatives to fullfil the higher concerns, since there is no reason in it self to remove the ActiveRecord gem.

like image 103
Jonas Schubert Erlandsson Avatar answered Nov 08 '22 04:11

Jonas Schubert Erlandsson