我有一个标准的postgresql database.yml配置。我需要生成一个数据库URL,以便在其他地方使用相同的参数。我认为rails有办法还是?

development:
  adapter: postgresql
  encoding: utf8
  host: localhost
  username: postgres
  password: postgres
  pool: 10
  reaping_frequency: 10
  connect_timeout: 5 # sec
  checkout_timeout: 5 # sec
  template: template0
  schema_search_path: "public,extensions"

另一方面,我发现这一个做的有点相反https://gist.github.com/pricees/9630464

最佳答案

也许这能帮上忙?
Get DATABASE_URL from config hash
你给它一个散列(字符串或符号键,无关紧要),它会给你一个URL:

>> c = YAML.load_file('/path/to/config/database.yml')
=> {"development"=>{"adapter"=>"mysql2", "database"=>"myapp_development", "username"=>"root", "password"=>"password", "encoding"=>"utf8"}, "test"=>{"adapter"=>"mysql2", "database"=>"myapp_test", "username"=>"root", "password"=>"password", "encoding"=>"utf8"}, "production"=>{"adapter"=>"mysql2", "database"=>"myapp", "username"=>"myapp", "password"=>"XXX", "encoding"=>"utf8"}, "cucumber"=>{"adapter"=>"mysql2", "database"=>"myapp_cucumber", "username"=>"root", "password"=>"password", "encoding"=>"utf8"}}
>> DatabaseUrl.to_active_record_url c['development']
=> "mysql2://root:[email protected]/myapp_development?encoding=utf8"

10-06 07:00