Hi everyone Is there a way to access the information in db/database.yml at runtime. Sort of a DB_ENV or something similar? Kindest regards Erik Lindblad --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Have you tried RAILS_ENV? It represents the environment in which you''re currently running: development, test or production. Of course, you can use more or different environments than that by making your own, e.g., staging. Regards, Craig --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 27 May 2008, at 07:53, Erik Lindblad wrote:> > Hi everyone > > Is there a way to access the information in db/database.yml at > runtime. Sort of a DB_ENV or something similar? >This is grungy (because there is no accessor for the instance variable. might not work for all adapters, might break between rails versions etc...) ActiveRecord::Base.connection.instance_variable_get(''@config'') If different models have different settings, then say Person.connection.instance_variable_get(''@config'') will get that models configuration Fred> Kindest regards > > Erik Lindblad > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Great advice, this really works. Craig, thanks for the advice but I really wanted to get the actual values set in database.yml and not the environment itself. Is there a reason for not putting this in RAILS_ENV or any similar construct? Or is it "un-Raily" to want to know what goes on behind the curtains. Great advice though, it will work for now. Regards Erik Lindblad On 27 Maj, 15:29, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 27 May 2008, at 07:53, Erik Lindblad wrote: > > > > > Hi everyone > > > Is there a way to access the information in db/database.yml at > > runtime. Sort of a DB_ENV or something similar? > > This is grungy (because there is no accessor for the instance > variable. might not work for all adapters, might break between rails > versions etc...) > > ActiveRecord::Base.connection.instance_variable_get(''@config'') > > If different models have different settings, then say > Person.connection.instance_variable_get(''@config'') > > will get that models configuration > > Fred > > > Kindest regards > > > Erik Lindblad--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Erik, Erik Lindblad wrote:> I really wanted to get the actual values set in database.ymlYou can do this quite easily by just reading database.yml. Something like... db_config YAML.load(File.open("#{RAILS_ROOT}/config/database.yml"))[ENV[''RAILS_ENV'']] HTH, Bill --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Here''s how I can get the username, password, etc from database.yml: config = Rails::Configuration.new database = config.database_configuration[”production”] Hope that helps. On May 27, 9:50 pm, "Bill Walton" <bill.wal...-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote:> Hi Erik, > > Erik Lindblad wrote: > > I really wanted to get the actual values set in database.yml > > You can do this quite easily by just reading database.yml. Something > like... > > db_config > YAML.load(File.open("#{RAILS_ROOT}/config/database.yml"))[ENV[''RAILS_ENV'']] > > HTH, > Bill--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ActiveRecord::Base.configurations returns a hash of the known configurations.>> ActiveRecord::Base.configurations[''production'']=> {"adapter"=>"postgresql", "host"=>"localhost", "database"=>"app_production"}>>- Gabriel On Tue, May 27, 2008 at 1:53 AM, Erik Lindblad <erik-gTO0lQh45js@public.gmane.org> wrote:> > Hi everyone > > Is there a way to access the information in db/database.yml at > runtime. Sort of a DB_ENV or something similar? > > Kindest regards > > Erik Lindblad > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi all This last one I think I like the best, more clean. Thanks for all the input /Erik On 28 Maj, 00:41, KiranaTama <ikinwira...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Here''s how I can get the username, password, etc from database.yml: > > config = Rails::Configuration.new > database = config.database_configuration[”production”] > > Hope that helps. > > On May 27, 9:50 pm, "Bill Walton" <bill.wal...-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote: > > > Hi Erik, > > > ErikLindbladwrote: > > > I really wanted to get the actual values set in database.yml > > > You can do this quite easily by just reading database.yml. Something > > like... > > > db_config > > YAML.load(File.open("#{RAILS_ROOT}/config/database.yml"))[ENV[''RAILS_ENV'']] > > > HTH, > > Bill--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---