Can you get DB username, pw, database name in Rails?

前端 未结 5 1904
感动是毒
感动是毒 2020-12-12 11:54

I\'m writing a rake task that does some DB work outside of Rails/ActiveRecord.

Is there a way to get the DB connection info (host, username, password, DB name) for t

5条回答
  •  长情又很酷
    2020-12-12 12:18

    ActiveRecord::Base.connection_config
    

    returns the connection configuration in a hash:

    => {:adapter=>ADAPTER_NAME, :host=>HOST, :port=>PORT, 
        :database=>DB, :pool=>POOL, :username=>USERNAME, 
        :password=>PASSWORD} 
    

    As tpett remarked in their comment: this solution accounts for merging the configuration from database.yml and from the environment variable DATABASE_URL.

提交回复
热议问题