{"id":4033,"date":"2014-02-24T20:15:07","date_gmt":"2014-02-24T11:15:07","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=4033"},"modified":"2018-11-14T16:33:52","modified_gmt":"2018-11-14T07:33:52","slug":"establish_connection%e3%81%a7database-yml%e3%81%ab%e5%ae%9a%e7%be%a9%e6%b8%88%e3%81%bf%e3%81%ae%e6%83%85%e5%a0%b1%e3%82%92%e5%a4%89%e6%9b%b4%e3%81%97%e3%81%a6%e4%bd%bf%e3%81%86","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2014\/02\/24\/establish_connection%e3%81%a7database-yml%e3%81%ab%e5%ae%9a%e7%be%a9%e6%b8%88%e3%81%bf%e3%81%ae%e6%83%85%e5%a0%b1%e3%82%92%e5%a4%89%e6%9b%b4%e3%81%97%e3%81%a6%e4%bd%bf%e3%81%86\/","title":{"rendered":"establish_connection\u3067database.yml\u306b\u5b9a\u7fa9\u6e08\u307f\u306e\u60c5\u5831\u3092\u5909\u66f4\u3057\u3066\u4f7f\u3046"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u6cb3\u91ce\u3067\u3059\u3002<\/p>\n<p>Rails\u3067establish_connection\u3092\u4f7f\u3046\u969b\u306b\u306f\u3001config\/database.yml\u306b\u8a2d\u5b9a\u6e08\u307f\u306e\u5185\u5bb9\u304c\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><a href=\"http:\/\/www.techscore.com\/blog\/2012\/12\/12\/rails%E3%81%A7%E8%A4%87%E6%95%B0%E3%81%AE%E3%83%87%E3%83%BC%E3%82%BF%E3%83%99%E3%83%BC%E3%82%B9%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%99%E3%82%8B\/\">\u00bb Rails\u3067\u8907\u6570\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u4f7f\u7528\u3059\u308b TECHSCORE BLOG<\/a><br \/>\n\u3067\u9234\u6728\u3055\u3093\u304c\u66f8\u3044\u3066\u304f\u308c\u3066\u3044\u307e\u3059\u304c\u3001\u4e00\u90e8\u5f15\u7528\u3057\u307e\u3059\u3068\u3001<\/p>\n<pre class=\"lang:default decode:true \">development:\r\n  ...\r\n\r\nother_database:\r\n  adapter: postgresql\r\n  encoding: unicode\r\n  database: other_database\r\n  pool: 5\r\n  username: USERNAME\r\n  password: PASSWORD\r\n  host: localhost\r\n\r\ntest:\r\n  ...\r\n\r\nproduction:\r\n  ...<\/pre>\n<pre class=\"lang:ruby decode:true\">class User &lt; ActiveRecord::Base\r\n\r\n  establish_connection :other_database\r\n\r\nend<\/pre>\n<p>\u3053\u3093\u306a\u611f\u3058\u3067\u3059\u306d\u3002<\/p>\n<p>\u3067\u3059\u304c\u3001\u57fa\u672c\u306e\u8a2d\u5b9a\u306f\u305d\u306e\u307e\u307e\u3067\u3001\u52d5\u7684\u306b\u4e00\u90e8\u306e\u60c5\u5831\u3092\u5909\u3048\u305f\u3044\u3068\u304b\u3001\u305d\u3046\u3044\u3046\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u30e6\u30fc\u30b6\u540d\u3068\u30d1\u30b9\u30ef\u30fc\u30c9\u3060\u3051\u5909\u66f4\u3059\u308b\u3068\u304b\u3002<\/p>\n<h2>\u5b9a\u7fa9\u6e08\u307f\u306e\u60c5\u5831\u3092\u30cf\u30c3\u30b7\u30e5\u3068\u3057\u3066\u53d6\u5f97\u3059\u308b<\/h2>\n<p>ActiveRecord::Base.configurations\u3092\u4f7f\u7528\u3059\u308b\u3068\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true \">\r\n % bundle exec rails c\r\n[1] pry(main)> ActiveRecord::Base.configurations\r\n=> {\"development\"=>\r\n  {\"host\"=>\"localhost\",\r\n   \"adapter\"=>\"postgresql\",\r\n   \"encoding\"=>\"unicode\",\r\n   \"database\"=>\"hoge_development\",\r\n   \"pool\"=>5,\r\n   \"username\"=>\"USERNAME\",\r\n   \"password\"=>\"PASSWORD\"},\r\n \"development_other\"=>\r\n  {\"host\"=>\"other_host\",\r\n   \"adapter\"=>\"postgresql\",\r\n   \"encoding\"=>\"unicode\",\r\n   \"database\"=>\"hoge_development_other\",\r\n   \"pool\"=>5,\r\n   \"username\"=>\"USERNAME\",\r\n   \"password\"=>\"PASSWORD\"},\r\n...\r\n<\/pre>\n<p>\u30cf\u30c3\u30b7\u30e5\u306e\u30ad\u30fc\u306f\u6587\u5b57\u5217\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u3088\u3046\u3067\u3059\u3002<\/p>\n<h2>\u5fc5\u8981\u306a\u90e8\u5206\u3092\u66f8\u304d\u63db\u3048\u3066\u4f7f\u3046<\/h2>\n<p>\u63a5\u7d9a\u60c5\u5831\u304c\u30cf\u30c3\u30b7\u30e5\u3067\u53d6\u5f97\u3067\u304d\u308c\u3070\u3001\u5fc5\u8981\u306a\u90e8\u5206\u3092\u66f8\u304d\u63db\u3048\u308b\u3060\u3051\u3067\u3059\u306d\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">class User &lt; ActiveRecord::Base\r\n\r\n  def self.new_connection_info\r\n    connection_info = self.configrations[\"development_other\"]\r\n    connection_info[\"username\"] = \"NEW_USERNAME\"\r\n    connection_info\r\n  end\r\n\r\n  establish_connection new_connection_info\r\n\r\nend<\/pre>\n<p>\u5b9f\u969b\u306b\u306f\u3001\u3082\u3063\u3068\u30b9\u30de\u30fc\u30c8\u306a\u611f\u3058\u306b\u3067\u304d\u305d\u3046\u3067\u3059\u304c\u3001\u3068\u308a\u3042\u3048\u305a\u306e\u6d41\u308c\u3068\u3057\u3066\u306f\u3053\u3093\u306a\u611f\u3058\u304b\u306a\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u6cb3\u91ce\u3067\u3059\u3002<\/p>\n<p>Rails\u3067establish_connection\u3092\u4f7f\u3046\u969b\u306b\u306f\u3001config\/database.yml\u306b\u8a2d\u5b9a\u6e08\u307f\u306e\u5185\u5bb9\u304c\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2014\/02\/24\/establish_connection%e3%81%a7database-yml%e3%81%ab%e5%ae%9a%e7%be%a9%e6%b8%88%e3%81%bf%e3%81%ae%e6%83%85%e5%a0%b1%e3%82%92%e5%a4%89%e6%9b%b4%e3%81%97%e3%81%a6%e4%bd%bf%e3%81%86\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[22,24],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/4033"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=4033"}],"version-history":[{"count":10,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/4033\/revisions"}],"predecessor-version":[{"id":17880,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/4033\/revisions\/17880"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=4033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=4033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=4033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}