{"id":1041,"date":"2012-12-12T00:04:50","date_gmt":"2012-12-11T15:04:50","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=1041"},"modified":"2018-11-14T16:33:58","modified_gmt":"2018-11-14T07:33:58","slug":"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","status":"publish","type":"post","link":"https:\/\/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\/","title":{"rendered":"Rails\u3067\u8907\u6570\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u4f7f\u7528\u3059\u308b"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>Ruby on Rails \u3067\u8907\u6570\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u6271\u3046\u65b9\u6cd5\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>establish_connection<\/h2>\n<p>establish_connection \u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u63a5\u7d9a\u3059\u308b\u305f\u3081\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<\/p>\n<p>\u901a\u5e38\u306f\u660e\u793a\u7684\u306b\u547c\u3073\u51fa\u3059\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u304c\u3001\u8907\u6570\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u660e\u793a\u7684\u306b\u547c\u3073\u51fa\u3059\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">class User &lt; ActiveRecord::Base\r\n\r\n    establish_connection :adapter  =&gt; 'postgresql',\r\n                         :encoding =&gt; 'unicode',\r\n                         :database =&gt; 'other_database',\r\n                         :pool     =&gt; '5',\r\n                         :username =&gt; 'USERNAME',\r\n                         :password =&gt; 'PASSWORD',\r\n                         :host     =&gt; 'localhost'\r\n\r\nend<\/pre>\n<p>\u5f15\u6570\u306b\u6307\u5b9a\u3067\u304d\u308b\u30aa\u30d7\u30b7\u30e7\u30f3\u306f\u3001config\/database.yml \u3067\u6307\u5b9a\u3059\u308b\u5185\u5bb9\u3068\u540c\u3058\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u63a5\u7d9a\u5148\u306e\u60c5\u5831\u3092 database.yml \u306b\u66f8\u3044\u3066\u304a\u304f<\/h2>\n<p>config\/database.yml \u306e\u5185\u5bb9\u304c\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u308b\u3068\u3057\u307e\u3059\u3002<\/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<p>other_database \u306e\u90e8\u5206\u3060\u3051\u3057\u3063\u304b\u308a\u66f8\u3044\u3066\u3044\u308b\u7406\u7531\u306f\u3001\u3053\u308c\u304b\u3089\u305d\u3053\u306b\u63a5\u7d9a\u3059\u308b\u304b\u3089\u3067\u3059\u3002<\/p>\n<p>\u305d\u308c\u3067\u306f\u63a5\u7d9a\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\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\u3053\u3067\u306e\u30dd\u30a4\u30f3\u30c8\u306f\u3001establish_connection \u306e\u5f15\u6570\u306b :other_database \u3068\u3044\u3046\u30b7\u30f3\u30dc\u30eb\u3092\u6e21\u3057\u3066\u3044\u308b\u3068\u3053\u308d\u3067\u3059\u3002<\/p>\n<p>establish_connection \u306f\u5f15\u6570\u306b\u6587\u5b57\u5217\u3084\u30b7\u30f3\u30dc\u30eb\u304c\u6e21\u3055\u308c\u305f\u5834\u5408\u3001config\/database.yml \u306e\u5185\u5bb9\u3092\u53c2\u7167\u3057\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306b\u3053\u306e\u65b9\u5f0f\u3092\u5229\u7528\u3059\u308b\u5834\u5408\u306f :other_database \u306e\u3088\u3046\u306a\u6c7a\u3081\u6253\u3061\u3067\u306f\u306a\u304f\u3001\u300cdevelopment_other_database\u300d\u3084\u300ctest_other_database\u300d\u306e\u3088\u3046\u306b\u74b0\u5883\u3054\u3068\u306b\u5b9a\u7fa9\u3057\u3066\u304a\u304d\u3001\u300cestablish_connection \"#{Rails.env}_other_database\"\u300d\u306e\u3088\u3046\u306b\u53c2\u7167\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u540c\u3058\u30e2\u30c7\u30eb\u3067\u3082\u72b6\u6cc1\u306b\u3088\u3063\u3066\u63a5\u7d9a\u5148\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304c\u7570\u306a\u308b\u5834\u5408<\/h2>\n<p>\u540c\u3058\u30e2\u30c7\u30eb\u3067\u3082\u72b6\u6cc1\u306b\u3088\u3063\u3066\u63a5\u7d9a\u5148\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304c\u7570\u306a\u308b\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u306e\u5916\u304b\u3089 establish_connection \u3092\u547c\u3073\u51fa\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">User.establish_connection :adapter  =&gt; 'postgresql',\r\n                          :encoding =&gt; 'unicode',\r\n                          :database =&gt; 'other_database',\r\n                          :pool     =&gt; '5',\r\n                          :username =&gt; 'USERNAME',\r\n                          :password =&gt; 'PASSWORD',\r\n                          :host     =&gt; 'localhost'<\/pre>\n<p>\u3053\u306e\u65b9\u5f0f\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u306e\u30e6\u30fc\u30b6\u3054\u3068\u306b\u53c2\u7167\u5148\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3084\u30b9\u30ad\u30fc\u30de\u304c\u7570\u306a\u308b\u5834\u5408\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u63a5\u7d9a\u60c5\u5831\u306f\u30b5\u30d6\u30af\u30e9\u30b9\u306b\u5f15\u304d\u7d99\u304c\u308c\u308b<\/h2>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u63a5\u7d9a\u60c5\u5831\u306f\u30b5\u30d6\u30af\u30e9\u30b9\u306b\u5f15\u304d\u7d99\u304c\u308c\u307e\u3059\u3002<\/p>\n<p>\u5177\u4f53\u4f8b\u3068\u3057\u3066\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u898b\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">class TaroDatabaseModel &lt; ActiveRecord::Base\r\n  self.abstract_class = true\r\n  establish_connection :taro_database\r\nend\r\n\r\nclass HanakoDatabaseModel &lt; ActiveRecord::Base\r\n  self.abstract_class = true\r\n  establish_connection :hanako_database\r\nend\r\n\r\nclass TaroUser &lt; TaroDatabaseModel\r\n\r\nend\r\n\r\nclass HanakoUser &lt; HanakoDatabaseModel\r\n\r\nend<\/pre>\n<p>TaroUser \u3068 HanakoUser \u306f\u305d\u308c\u305e\u308c\u3001\u3069\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u53c2\u7167\u3059\u308b\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>\u6b63\u89e3\u306f\u3001TaroUser \u306f taro_database\u3001HanakoUser \u306f hanako_database \u3092\u53c2\u7167\u3057\u307e\u3059\u3002<\/p>\n<p>\u305d\u306e\u7406\u7531\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u63a5\u7d9a\u60c5\u5831\u306f\u30b5\u30d6\u30af\u30e9\u30b9\u306b\u5f15\u304d\u7d99\u304c\u308c\u308b\u304b\u3089\u3067\u3059\u3002<\/p>\n<p>\u8a00\u3044\u63db\u3048\u308b\u3068\u3001\u4e0a\u8a18\u30b3\u30fc\u30c9\u306e\u3088\u3046\u306b taro_database \u3092\u53c2\u7167\u3059\u308b\u30e2\u30c7\u30eb\u306f\u5168\u3066 TaroDatabaseModel \u3092\u7d99\u627f\u3059\u308b\u3001hanako_database \u3092\u53c2\u7167\u3059\u308b\u30e2\u30c7\u30eb\u306f\u5168\u3066 HanakoDatabaseModel \u3092\u7d99\u627f\u3059\u308b\u3001\u3068\u3059\u308c\u3070\u3001establish_connection \u3092\u5168\u3066\u306e\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u3067\u547c\u3073\u51fa\u3059\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u00a0\u307e\u3068\u3081<\/h2>\n<p>\u307e\u3068\u3081\u3067\u3059\u3002<\/p>\n<ul>\n<li>establish_connection \u3067\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u63a5\u7d9a\u3059\u308b\u3002<\/li>\n<li>\u30aa\u30d7\u30b7\u30e7\u30f3\u306f database.yml \u306b\u8a18\u8ff0\u3059\u308b\u5185\u5bb9\u3068\u540c\u3058\u3002<\/li>\n<li>database.yml \u306e\u9805\u76ee\u3092\u53c2\u7167\u3059\u308b\u5834\u5408\u306f\u3001\u9805\u76ee\u540d\u3092\u6587\u5b57\u5217\u307e\u305f\u306f\u30b7\u30f3\u30dc\u30eb\u3067\u6e21\u3059\u3002<\/li>\n<li>\u30e2\u30c7\u30eb\u3054\u3068\u306b\u63a5\u7d9a\u5148\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304c\u6c7a\u307e\u308b\u5834\u5408\u306f\u3001\u30af\u30e9\u30b9\u5b9a\u7fa9\u306e\u5185\u5074\u3067 establish_connection \u3092\u547c\u3073\u51fa\u3059\u3002<\/li>\n<li>\u540c\u3058\u30e2\u30c7\u30eb\u3067\u3082\u72b6\u6cc1\u306b\u5fdc\u3058\u3066\u63a5\u7d9a\u5148\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304c\u7570\u306a\u308b\u5834\u5408\u306f\u3001User.establish_connection \u306e\u3088\u3046\u306b\u5916\u90e8\u304b\u3089\u547c\u3073\u51fa\u3059\u3068\u826f\u3044\u3002<\/li>\n<li>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u63a5\u7d9a\u60c5\u5831\u306f\u30b5\u30d6\u30af\u30e9\u30b9\u306b\u5f15\u304d\u7d99\u304c\u308c\u308b\uff08\u5171\u901a\u306e\u89aa\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3059\u308b\u3068\u697d\uff09\u3002<\/li>\n<\/ul>\n<p>Enjoy Rails!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>Ruby on Rails \u3067\u8907\u6570\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u6271\u3046\u65b9\u6cd5\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<br \/>\nestablish_connection<br \/>\nestablish_connection \u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u63a5\u7d9a\u3059\u308b\u305f\u3081\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<br \/><a href=\"https:\/\/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\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[22],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1041"}],"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\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=1041"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1041\/revisions"}],"predecessor-version":[{"id":10713,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1041\/revisions\/10713"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=1041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=1041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=1041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}