Skip to content

Instantly share code, notes, and snippets.

@phurni
Created May 16, 2014 21:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phurni/caac3efcbb43a38b8946 to your computer and use it in GitHub Desktop.
Save phurni/caac3efcbb43a38b8946 to your computer and use it in GitHub Desktop.
override active_record.initialize_database in rails (rails4)
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if ar_initdb = ActiveRecord::Railtie.initializers.find {|item| item.name == 'active_record.initialize_database'}
ActiveRecord::Railtie.initializers.delete(ar_initdb)
ActiveRecord::Railtie.initializer :after => ar_initdb.after do
ActiveSupport.on_load(:active_record) do
self.configurations = Rails.application.config.database_configuration
begin
establish_connection
rescue ActiveRecord::NoDatabaseError, ActiveRecord::AdapterNotSpecified, ActiveRecord::AdapterNotFound
if app.config.database_not_required
app.config.middleware.delete "ActiveRecord::QueryCache"
app.config.middleware.delete "ActiveRecord::ConnectionAdapters::ConnectionManagement"
else
warn <<-end_warning
Oops - You have a database configured, but it doesn't exist yet!
Here's how to get started:
1. Configure your database in config/database.yml.
2. Run `bin/rake db:create` to create the database.
3. Run `bin/rake db:setup` to load your database schema.
end_warning
raise
end
end
end
end
end
# Rest of your application.rb file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment