SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    override the database.lyml

    hi...

    i'm trying to override the database.yml. is it possible to insert a variable there, lets say the username and the password?

    thanks!!!!

  2. #2
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <%= %> tags seem to work, giving you access to whatever Ruby you wanted to throw into database.yml.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  3. #3
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi....

    i tried smthg like this
    username: <%= @session[:user].login %>
    but i still got the error messg "rails could not connect to your database."

    thanks 4 ur help

  4. #4
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You won't be able to access anything in the normal Rails environment in your database.yml file - all you'll be able to use is normal Ruby.

  5. #5
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry, but i didn't get it.
    do i hv to do it in the controller?

  6. #6
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by masuk
    sorry, but i didn't get it.
    do i hv to do it in the controller?
    I'm not really sure if you can (or why you would want to) do what you are trying to do. You connect to your database and that is it. There are ways of connecting to more than database if you need to but to dynamically connect to a database the only thing you could do was wire the activerecord connection statement into one of your models, replacing actual values with class attributes (@@some_class_var) and setting those in your application controller.

    Code:
    # in application.rb
    MyFancyModel.db_user = 'foo'
    MyFancyModel.db_pass = 'bar'
    
    class MyFancyModel < ActiveRecord::Base
      establish_connection(
        :adapter => 'mysql',
        :username => @@db_user,
        :password => @@db_pass
      )
    end
    The above is just an example and I haven't tested it!

    You could then create models that use the above connection by extending MyFancyModel instead of AR::Base.

    http://api.rubyonrails.com/classes/A...e.html#M000797


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •