Friday, February 5, 2010

How To Run My Rails Application Without Database?

A day before haven't tried or had thought about how to disable DB connection when the rails application loads, but unfortunately one of my colleague asked me "Hey, How To Run My Rails Application Without Database?". So i worked on this to get myself aware of this scenario.

Before you understand, ask yourself "Why do you want to disable a DB for rails application?" Obviously the answer will be, Some sites doesn't need DB actually, So i will let you guys see what i did for this scenario, Actually we have two ways to do.

First and Simple Way of Disabling DB:
on 21st line of /config/environment.rb uncomment.
config.frameworks -= [ :active_record, :active_resource, :action_mailer ]


Second way of Disabling DB:
First Step:
#config/environment.rb
require File.join(File.dirname(__FILE__), 'boot')


Second Step:
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

Third Step:
#now, instead of `Rails::Initializer.run ...` do this
MyInitializer.run do |config|
# ...
end

That is it. Now you can run your rails application without DB Connection.
Hope this helps you, pls do a comment

Thanks,
Bala, Anubavam Technologies.

No comments:

Post a Comment