Run Rails App without DB Connection
One of my co rails developer asked me how to disable DB connection when the rails application loads. Before you understand, ask yourself why you want to disable a database when rails app load. Some of the application might just deal with other web services – such as twitter apps. They might never need the database. Let’s look at how to disable the database when the rails application load.
Step 1:
#config/environment.rb
require File.join(File.dirname(__FILE__), 'boot')
Step 2:
class MyInitializer < Rails::Initializer
def initialize_database
if configuration.frameworks.include?(:active_record)
ActiveRecord::Base.configurations = configuration.database_configuration
## uncomment the next line to automatically establish the connection
# ActiveRecord::Base.establish_connection
end
end
end
Step 3:
#now, instead of `Rails::Initializer.run ...` do this
MyInitializer.run do |config|
# ...
end
Easy huh! Go ahead and run your rails application without Database Connection.
- bala's blog
- Login or register to post comments

