activerecord · multiple-models · Rails · rails-environments · rubyonrails

How to find different environment configurations in rails

While working with a multiple-database application I came across the point where I had to juggle between different environment configurations.

I had to pass connection hash to ActiveRecord::Base.establish_connection, something like this:

ActiveRecord::Base.establish_connection
(
  :adapter=>"mysql",
  :host=>"localhost",
  :username=>"myuser",
  :password=>"mypass",
  :database => "somedatabase"
)

This connection can be easily retrieved from ActiveRecord::Base.configurations, this is currently undocumented.

Using ActiveRecord::Base.configurations, the above can be simply written as:

ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['production'])
Advertisements

One thought on “How to find different environment configurations in rails

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s