{"id":16,"date":"2009-05-18T22:07:51","date_gmt":"2009-05-18T15:07:51","guid":{"rendered":"http:\/\/ibatt.info\/?p=16"},"modified":"2009-10-02T23:36:23","modified_gmt":"2009-10-02T16:36:23","slug":"multiple-database-connection-on-rails","status":"publish","type":"post","link":"http:\/\/ibatt.info\/web\/?p=16","title":{"rendered":"Multiple Database connection on Rails"},"content":{"rendered":"<p>\u0e40\u0e01\u0e34\u0e48\u0e19\u0e19\u0e33\u0e0b\u0e31\u0e01\u0e40\u0e25\u0e47\u0e01\u0e19\u0e49\u0e2d\u0e22\u0e41\u0e25\u0e49\u0e27\u0e01\u0e31\u0e19&#8230;<\/p>\n<p>\u0e17\u0e33\u0e07\u0e32\u0e19\u0e43\u0e0a\u0e49 ROR \u0e21\u0e32\u0e44\u0e14\u0e49\u0e0b\u0e31\u0e01\u0e1e\u0e31\u0e01 \u0e44\u0e14\u0e49\u0e17\u0e33 Project GSMIS \u0e02\u0e2d\u0e07\u0e1a\u0e31\u0e13\u0e11\u0e34\u0e15\u0e27\u0e34\u0e17\u0e22\u0e32\u0e25\u0e31\u0e22 \u0e21\u0e2b\u0e32\u0e27\u0e34\u0e17\u0e22\u0e32\u0e25\u0e31\u0e22\u0e02\u0e2d\u0e19\u0e41\u0e01\u0e48\u0e19<\/p>\n<p>\u0e42\u0e2d\u0e49\u0e27\u0e27\u0e27&#8230;!!!!!!!! \u0e40\u0e08\u0e2d\u0e2d\u0e30\u0e44\u0e23\u0e17\u0e35\u0e48\u0e15\u0e49\u0e2d\u0e07 research \u0e40\u0e22\u0e2d\u0e30\u0e21\u0e32\u0e01<\/p>\n<p>\u0e40\u0e23\u0e37\u0e48\u0e2d\u0e07\u0e41\u0e23\u0e01\u0e40\u0e25\u0e22&#8230;\u0e10\u0e32\u0e19\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25\u0e40\u0e23\u0e32\u0e44\u0e21\u0e48\u0e21\u0e35\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25\u0e17\u0e35\u0e48\u0e40\u0e01\u0e47\u0e1a\u0e44\u0e27\u0e49\u0e40\u0e2d\u0e07 \u0e15\u0e49\u0e2d\u0e07 import<\/p>\n<p>\u0e01\u0e48\u0e2d\u0e19\u0e2b\u0e19\u0e49\u0e32\u0e19\u0e35\u0e49 Project \u0e19\u0e35\u0e49\u0e1c\u0e48\u0e32\u0e19\u0e21\u0e37\u0e2d\u0e1e\u0e35\u0e48\u0e04\u0e19\u0e2d\u0e37\u0e48\u0e19\u0e21\u0e32\u0e01\u0e48\u0e2d\u0e19\u0e01\u0e48\u0e2d\u0e19\u0e08\u0e30\u0e21\u0e32\u0e16\u0e36\u0e07\u0e40\u0e23\u0e32<\/p>\n<p>\u0e15\u0e2d\u0e19\u0e19\u0e31\u0e49\u0e19&#8230;\u0e17\u0e38\u0e01\u0e04\u0e19\u0e22\u0e31\u0e07\u0e43\u0e2b\u0e21\u0e48\u0e01\u0e31\u0e1a ROR \u0e40\u0e25\u0e22\u0e43\u0e0a\u0e49\u0e27\u0e34\u0e18\u0e35 import \u0e08\u0e32\u0e01 Excel \u0e44\u0e14\u0e49\u0e1c\u0e25\u0e14\u0e35\u0e43\u0e19\u0e23\u0e30\u0e14\u0e31\u0e1a<\/p>\n<p>\u0e21\u0e35\u0e02\u0e49\u0e2d\u0e40\u0e2a\u0e35\u0e22\u0e04\u0e37\u0e2d\u0e40\u0e1b\u0e25\u0e37\u0e2d\u0e07\u0e40\u0e27\u0e25\u0e32\u0e04\u0e19\u0e21\u0e32\u0e19\u0e31\u0e48\u0e07 upload file<\/p>\n<p>\u0e41\u0e25\u0e30\u0e41\u0e25\u0e49\u0e27 GSMIS \u0e01\u0e47\u0e44\u0e14\u0e49\u0e40\u0e23\u0e34\u0e48\u0e21\u0e19\u0e31\u0e1a\u0e2b\u0e19\u0e36\u0e48\u0e07\u0e43\u0e2b\u0e21\u0e48\u0e2d\u0e35\u0e01\u0e04\u0e23\u0e31\u0e49\u0e07 \u0e17\u0e33\u0e43\u0e2b\u0e49\u0e44\u0e14\u0e49\u0e17\u0e33 research \u0e40\u0e23\u0e37\u0e48\u0e2d\u0e07\u0e41\u0e23\u0e01 \u0e04\u0e37\u0e2d<\/p>\n<p><strong>&#8220;Multiple Database connection on Rails&#8221;<\/strong><\/p>\n<p>\u0e2b\u0e25\u0e31\u0e07\u0e08\u0e32\u0e01\u0e40\u0e02\u0e35\u0e22\u0e19\u0e19\u0e49\u0e33\u0e21\u0e32\u0e40\u0e22\u0e2d\u0e30 \u0e21\u0e32\u0e08\u0e23\u0e34\u0e07\u0e08\u0e31\u0e01\u0e01\u0e31\u0e19\u0e44\u0e14\u0e49\u0e41\u0e25\u0e49\u0e27<\/p>\n<p>\u0e16\u0e49\u0e32\u0e40\u0e23\u0e32\u0e08\u0e33\u0e40\u0e1b\u0e47\u0e19\u0e15\u0e49\u0e2d\u0e07\u0e43\u0e0a\u0e49 <strong>Database \u0e21\u0e32\u0e01\u0e01\u0e27\u0e48\u0e32 1 \u0e15\u0e31\u0e27 \u0e43\u0e19 1 Project<\/strong><\/p>\n<p><strong>\u0e02\u0e31\u0e49\u0e19\u0e41\u0e23\u0e01<\/strong>&#8230;\u0e41\u0e01\u0e49 config \u0e17\u0e35\u0e48\u0e44\u0e1f\u0e25\u0e4c config\/database.yml<\/p>\n<p><strong>\u0e01\u0e48\u0e2d\u0e19\u0e41\u0e01\u0e49 config\/database.yml<\/strong><\/p>\n<blockquote>\n<pre> <span style=\"color: #993399;\"># Default<\/span>\r\n <span style=\"color: #996633;\"><strong>defaults<\/strong><\/span>: &amp;defaults\r\n   <span style=\"color: #66ccff;\">adapter<\/span>: postgresql\r\n   <span style=\"color: #66ccff;\">encoding<\/span>: unicode\r\n   <span style=\"color: #66ccff;\">host<\/span>: localhost\r\n   <span style=\"color: #66ccff;\">database<\/span>: databasename\r\n   <span style=\"color: #66ccff;\">username<\/span>: username\r\n   <span style=\"color: #66ccff;\">password<\/span>: password\r\n <span style=\"color: #996633;\"><strong>development<\/strong><\/span>:\r\n   <span style=\"color: #66ccff;\">&lt;&lt;<\/span>: *defaults\r\n <span style=\"color: #996633;\"><strong>test<\/strong><\/span>:\r\n   <span style=\"color: #66ccff;\">&lt;&lt;<\/span>: *defaults\r\n <span style=\"color: #996633;\"><strong>production<\/strong><\/span>:\r\n   <span style=\"color: #66ccff;\">&lt;&lt;<\/span>: *defaults<\/pre>\n<\/blockquote>\n<p>\u0e15\u0e32\u0e21\u0e02\u0e49\u0e32\u0e07\u0e1a\u0e19 \u0e01\u0e47\u0e08\u0e30\u0e40\u0e1b\u0e47\u0e19\u0e1b\u0e01\u0e15\u0e34\u0e17\u0e35\u0e48\u0e40\u0e23\u0e32\u0e43\u0e0a\u0e49\u0e46 \u0e01\u0e31\u0e19 \u0e04\u0e37\u0e2d connect \u0e44\u0e1b\u0e17\u0e35\u0e48 database \u0e40\u0e14\u0e35\u0e22\u0e27 \u0e15\u0e48\u0e2d\u0e44\u0e1b\u0e08\u0e30\u0e40\u0e1b\u0e47\u0e19 code \u0e2b\u0e25\u0e31\u0e07\u0e08\u0e32\u0e01\u0e41\u0e01\u0e49\u0e44\u0e02<\/p>\n<p><strong>\u0e41\u0e01\u0e49\u0e44\u0e02\u0e41\u0e25\u0e49\u0e27 config\/database.yml<\/strong><\/p>\n<blockquote>\n<pre> <span style=\"color: #993399;\"># Default<\/span>\r\n <span style=\"color: #996633;\"><strong>defaults<\/strong><\/span>: &amp;defaults\r\n   <span style=\"color: #66ccff;\">adapter<\/span>: postgresql\r\n   <span style=\"color: #66ccff;\">encoding<\/span>: unicode\r\n   <span style=\"color: #66ccff;\">host<\/span>: localhost\r\n   <span style=\"color: #66ccff;\">database<\/span>: databasename\r\n   <span style=\"color: #66ccff;\">username<\/span>: username\r\n   <span style=\"color: #66ccff;\">password<\/span>: password\r\n <span style=\"color: #996633;\"><strong>development<\/strong><\/span>:\r\n   <span style=\"color: #66ccff;\">&lt;&lt;<\/span>: *defaults\r\n <span style=\"color: #996633;\"><strong>test<\/strong><\/span>:\r\n   <span style=\"color: #66ccff;\">&lt;&lt;<\/span>: *defaults\r\n <span style=\"color: #996633;\"><strong>production<\/strong><\/span>:\r\n   <span style=\"color: #66ccff;\">&lt;&lt;<\/span>: *defaults\r\n\r\n <span style=\"color: #993399;\"># Database \u0e17\u0e35\u0e48 N<\/span>\r\n <span style=\"color: #996633;\"><strong>default_Ns<\/strong><\/span>: &amp;default_Ns\r\n   <span style=\"color: #66ccff;\">adapter<\/span>: postgresql\r\n   <span style=\"color: #66ccff;\">encoding<\/span>: unicode\r\n   <span style=\"color: #66ccff;\">host<\/span>: localhost\r\n   <span style=\"color: #66ccff;\">database<\/span>: databasename_N\r\n   <span style=\"color: #66ccff;\">username<\/span>: username_N\r\n   <span style=\"color: #66ccff;\">password<\/span>: password_N\r\n <span style=\"color: #996633;\"><strong>prefixN_development<\/strong><\/span>:\r\n   <span style=\"color: #66ccff;\">&lt;&lt;<\/span>: *default_Ns\r\n <span style=\"color: #996633;\"><strong>prefixN_test<\/strong><\/span>:\r\n   <span style=\"color: #66ccff;\">&lt;&lt;<\/span>: *default_Ns\r\n <span style=\"color: #996633;\"><strong>prefixN_production<\/strong><\/span>:\r\n   <span style=\"color: #66ccff;\">&lt;&lt;<\/span>: *default_Ns<\/pre>\n<\/blockquote>\n<p>\u0e1e\u0e2d\u0e41\u0e01\u0e49\u0e44\u0e02 config\/database.yml \u0e41\u0e25\u0e49\u0e27 \u0e22\u0e31\u0e07\u0e44\u0e21\u0e48\u0e2a\u0e32\u0e21\u0e32\u0e23\u0e16\u0e43\u0e0a\u0e49\u0e44\u0e14\u0e49 \u0e22\u0e31\u0e07\u0e15\u0e49\u0e2d\u0e07\u0e44\u0e1b\u0e41\u0e01\u0e49\u0e2d\u0e35\u0e01\u0e44\u0e1f\u0e25\u0e4c<\/p>\n<p>\u0e40\u0e2d\u0e49\u0e22\u0e22\u0e22..\u0e44\u0e21\u0e48\u0e2a\u0e34 \u0e40\u0e23\u0e35\u0e22\u0e01\u0e27\u0e48\u0e32 \u0e40\u0e02\u0e35\u0e22\u0e19 code \u0e40\u0e1e\u0e34\u0e48\u0e21\u0e14\u0e35\u0e01\u0e27\u0e48\u0e32<\/p>\n<p>\u0e44\u0e1f\u0e25\u0e4c\u0e17\u0e35\u0e48\u0e08\u0e30\u0e44\u0e1b\u0e40\u0e02\u0e35\u0e22\u0e19 code \u0e40\u0e1e\u0e34\u0e48\u0e21\u0e01\u0e47\u0e04\u0e37\u0e2d &#8230; \u0e44\u0e1f\u0e25\u0e4c controller \u0e44\u0e2b\u0e19\u0e01\u0e47\u0e44\u0e14\u0e49<\/p>\n<p>\u0e41\u0e15\u0e48\u0e08\u0e30\u0e1e\u0e32\u0e40\u0e02\u0e35\u0e22\u0e19\u0e44\u0e27\u0e49\u0e17\u0e35\u0e48 app\/controllers\/application.rb (\u0e04\u0e07\u0e23\u0e39\u0e49\u0e19\u0e30\u0e27\u0e48\u0e32\u0e17\u0e33\u0e44\u0e21 \u0e16\u0e36\u0e07\u0e40\u0e02\u0e35\u0e22\u0e19\u0e44\u0e27\u0e49\u0e17\u0e35\u0e48\u0e19\u0e35\u0e48)<\/p>\n<blockquote>\n<pre> <span style=\"color: #cc0099;\"><strong>class<\/strong><\/span> ApplicationController &lt; ActionController::Base\r\n\r\n  before_filter <span style=\"color: #ff0000;\"><strong>:set_N_db_connection<\/strong><\/span>\r\n\r\n  <span style=\"color: #cc0099;\"><strong>def<\/strong><\/span> set_N_db_connection\r\n\r\n   <span style=\"color: #339966;\"># extra_coord = YAML.load(File.open(File.join(RAILS_ROOT, PATH_FILE_CONFIG),\"r\"))[PREFIX_CONNECTION+ ENV['RAILS_ENV']]<\/span>\r\n   extra_coord = YAML.load(File.open(File.join(RAILS_ROOT,<span style=\"color: #0000ff;\">\"config\/database.yml\"<\/span>),<span style=\"color: #0000ff;\">\"r\"<\/span>))[,<span style=\"color: #0000ff;\">\"prefixN_\"<\/span>+ ENV['RAILS_ENV']]\r\n\r\n   <span style=\"color: #339966;\"># MODEL_CLASS_NAME.establish_connection(extra_coord)<\/span>\r\n   TableName.establish_connection(extra_coord)\r\n\r\n  <span style=\"color: #cc0099;\"><strong>end<\/strong><\/span>\r\n\r\n <span style=\"color: #cc0099;\"><strong>end<\/strong><\/span><\/pre>\n<\/blockquote>\n<p>TableName \u0e04\u0e37\u0e2d\u0e2d\u0e30\u0e44\u0e23 \u0e21\u0e31\u0e19\u0e01\u0e47\u0e04\u0e37\u0e2d class name \u0e02\u0e2d\u0e07 model \u0e19\u0e31\u0e48\u0e19\u0e40\u0e2d\u0e07<\/p>\n<p>\u0e40\u0e1e\u0e23\u0e32\u0e30\u0e09\u0e30\u0e19\u0e31\u0e49\u0e19 \u0e40\u0e23\u0e32\u0e01\u0e47\u0e15\u0e49\u0e2d\u0e07\u0e21\u0e35 model TableName (app\/model\/table_name.rb) \u0e14\u0e49\u0e27\u0e22<\/p>\n<p>\u0e40\u0e17\u0e48\u0e32\u0e19\u0e35\u0e49\u0e01\u0e47\u0e40\u0e23\u0e35\u0e22\u0e1a\u0e23\u0e49\u0e2d\u0e22<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0e40\u0e01\u0e34\u0e48\u0e19\u0e19\u0e33\u0e0b\u0e31\u0e01\u0e40\u0e25\u0e47\u0e01\u0e19\u0e49\u0e2d\u0e22\u0e41\u0e25\u0e49\u0e27\u0e01\u0e31\u0e19&#8230; \u0e17\u0e33\u0e07\u0e32\u0e19\u0e43\u0e0a\u0e49 ROR \u0e21\u0e32\u0e44\u0e14\u0e49\u0e0b\u0e31\u0e01\u0e1e\u0e31\u0e01 \u0e44\u0e14\u0e49\u0e17\u0e33 Project GSMIS \u0e02\u0e2d\u0e07\u0e1a\u0e31\u0e13\u0e11\u0e34\u0e15\u0e27\u0e34\u0e17\u0e22\u0e32\u0e25\u0e31\u0e22 \u0e21\u0e2b\u0e32\u0e27\u0e34\u0e17\u0e22\u0e32\u0e25\u0e31\u0e22\u0e02\u0e2d\u0e19\u0e41\u0e01\u0e48\u0e19 \u0e42\u0e2d\u0e49\u0e27\u0e27\u0e27&#8230;!!!!!!!! \u0e40\u0e08\u0e2d\u0e2d\u0e30\u0e44\u0e23\u0e17\u0e35\u0e48\u0e15\u0e49\u0e2d\u0e07 research \u0e40\u0e22\u0e2d\u0e30\u0e21\u0e32\u0e01 \u0e40\u0e23\u0e37\u0e48\u0e2d\u0e07\u0e41\u0e23\u0e01\u0e40\u0e25\u0e22&#8230;\u0e10\u0e32\u0e19\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25\u0e40\u0e23\u0e32\u0e44\u0e21\u0e48\u0e21\u0e35\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25\u0e17\u0e35\u0e48\u0e40\u0e01\u0e47\u0e1a\u0e44\u0e27\u0e49\u0e40\u0e2d\u0e07 \u0e15\u0e49\u0e2d\u0e07 import \u0e01\u0e48\u0e2d\u0e19\u0e2b\u0e19\u0e49\u0e32\u0e19\u0e35\u0e49 Project \u0e19\u0e35\u0e49\u0e1c\u0e48\u0e32\u0e19\u0e21\u0e37\u0e2d\u0e1e\u0e35\u0e48\u0e04\u0e19\u0e2d\u0e37\u0e48\u0e19\u0e21\u0e32\u0e01\u0e48\u0e2d\u0e19\u0e01\u0e48\u0e2d\u0e19\u0e08\u0e30\u0e21\u0e32\u0e16\u0e36\u0e07\u0e40\u0e23\u0e32 \u0e15\u0e2d\u0e19\u0e19\u0e31\u0e49\u0e19&#8230;\u0e17\u0e38\u0e01\u0e04\u0e19\u0e22\u0e31\u0e07\u0e43\u0e2b\u0e21\u0e48\u0e01\u0e31\u0e1a ROR \u0e40\u0e25\u0e22\u0e43\u0e0a\u0e49\u0e27\u0e34\u0e18\u0e35 import \u0e08\u0e32\u0e01 Excel \u0e44\u0e14\u0e49\u0e1c\u0e25\u0e14\u0e35\u0e43\u0e19\u0e23\u0e30\u0e14\u0e31\u0e1a \u0e21\u0e35\u0e02\u0e49\u0e2d\u0e40\u0e2a\u0e35\u0e22\u0e04\u0e37\u0e2d\u0e40\u0e1b\u0e25\u0e37\u0e2d\u0e07\u0e40\u0e27\u0e25\u0e32\u0e04\u0e19\u0e21\u0e32\u0e19\u0e31\u0e48\u0e07 upload file \u0e41\u0e25\u0e30\u0e41\u0e25\u0e49\u0e27 GSMIS \u0e01\u0e47\u0e44\u0e14\u0e49\u0e40\u0e23\u0e34\u0e48\u0e21\u0e19\u0e31\u0e1a\u0e2b\u0e19\u0e36\u0e48\u0e07\u0e43\u0e2b\u0e21\u0e48\u0e2d\u0e35\u0e01\u0e04\u0e23\u0e31\u0e49\u0e07 \u0e17\u0e33\u0e43\u0e2b\u0e49\u0e44\u0e14\u0e49\u0e17\u0e33 research \u0e40\u0e23\u0e37\u0e48\u0e2d\u0e07\u0e41\u0e23\u0e01 \u0e04\u0e37\u0e2d &#8220;Multiple Database connection on Rails&#8221; \u0e2b\u0e25\u0e31\u0e07\u0e08\u0e32\u0e01\u0e40\u0e02\u0e35\u0e22\u0e19\u0e19\u0e49\u0e33\u0e21\u0e32\u0e40\u0e22\u0e2d\u0e30 \u0e21\u0e32\u0e08\u0e23\u0e34\u0e07\u0e08\u0e31\u0e01\u0e01\u0e31\u0e19\u0e44\u0e14\u0e49\u0e41\u0e25\u0e49\u0e27 \u0e16\u0e49\u0e32\u0e40\u0e23\u0e32\u0e08\u0e33\u0e40\u0e1b\u0e47\u0e19\u0e15\u0e49\u0e2d\u0e07\u0e43\u0e0a\u0e49 Database \u0e21\u0e32\u0e01\u0e01\u0e27\u0e48\u0e32 1 \u0e15\u0e31\u0e27 \u0e43\u0e19 1 Project \u0e02\u0e31\u0e49\u0e19\u0e41\u0e23\u0e01&#8230;\u0e41\u0e01\u0e49 config \u0e17\u0e35\u0e48\u0e44\u0e1f\u0e25\u0e4c config\/database.yml [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[9,10,7,11,8],"_links":{"self":[{"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=\/wp\/v2\/posts\/16"}],"collection":[{"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16"}],"version-history":[{"count":7,"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":19,"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=\/wp\/v2\/posts\/16\/revisions\/19"}],"wp:attachment":[{"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}