{S.A.Z.W.Q.A}

How to find different environment configurations in rails

October 10, 2008 · 1 Comment

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'])

Categories: Rails · activerecord · multiple-models · rails-environments · rubyonrails
Tagged: ,

1 response so far ↓

Leave a Comment