Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Auto-Detecting Database Adapter

The new version of the ar-extensions gem requires that you load the appropriate adapter yourself. On my development side I use mysql, however Heroku uses PostgreSQL.

For example, on my development side I need to do this:

require 'ar-extensions/adapters/mysql'
require 'ar-extensions/import/mysql'

How can I audo-detect which adapter to use?

like image 297
Dex Avatar asked Jun 06 '10 09:06

Dex


1 Answers

ActiveRecord::Base.configurations[Rails.env]['adapter'] will give you the name of the adapter in use.

The following code should do what you're after:

db_adapter = ActiveRecord::Base.configurations[Rails.env]['adapter']
require "ar-extensions/adapters/#{db_adapter}"
require "ar-extensions/import/#{db_adapter}"
like image 174
Jason Weathered Avatar answered Oct 01 '22 00:10

Jason Weathered