4
如果我想在rails中使用多個數據庫,我如何處理每個數據庫的單獨遷移數據庫?在Rails遷移中,如何處理多數據庫
在這種情況下,我有一個帳戶數據庫和其他數據庫的數據。
有類似:
遷移/賬戶/ 。 migrate/mydatabase/。
所以我可以運行獨立遷移。
如果我想在rails中使用多個數據庫,我如何處理每個數據庫的單獨遷移數據庫?在Rails遷移中,如何處理多數據庫
在這種情況下,我有一個帳戶數據庫和其他數據庫的數據。
有類似:
遷移/賬戶/ 。 migrate/mydatabase/。
所以我可以運行獨立遷移。
在你的database.yml創建的數據庫,如不同的連接方式:
development1:
adapter: mysql
username: root
password:
database: example_development1
development2:
adapter: mysql
username: root
password:
database: example_development2
然後給你選擇的每個數據庫中的每個模型,可以使用存儲:
class Account < ActiveRecord::Base
establish_connection :development2
end
編輯 如果您想要將其應用到可以執行的遷移:
class Migration1 < ActiveRecord::Migration
def self.connection
Account.connection #being Account a model that has a connection to the database you want
end
.....
end
這是一個很好的答案,這也是適用於遷移? – 2010-12-07 00:17:51