モデル
class Country < ActiveRecord::Base
attr_accessible :name
def self.table_name_prefix
'messages_development.'
end
establish_connection "development"
has_many :prefectures
scope :testname ,joins(:prefectures)
end
class Prefecture < ActiveRecord::Base
attr_accessible :name
def self.table_name_prefix
'messages_staging.'
end
establish_connection "staging"
belongs_to :country
end
database.yml
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: messages_development
pool: 5
username: root
password:
socket: /tmp/mysql.sock
staging:
adapter: mysql2
encoding: utf8
reconnect: false
database: messages_staging
pool: 5
username: root
password:
socket: /tmp/mysql.sock
ja = Country.create(name: "Japan")
INSERT INTO `messages_development`.`countries`
(`created_at`, `name`, `updated_at`)
VALUES
('2012-05-30 08:28:02', 'Japan', '2012-05-30 08:28:02')
pre = Prefecture.create(name: "Tokyo")
INSERT INTO `messages_staging`.`prefectures`
(`country_id`, `created_at`, `name`, `updated_at`)
VALUES
(NULL, '2012-05-30 08:30:17', 'Tokyo', '2012-05-30 08:30:17')
ja.prefectures << pre
UPDATE `messages_staging`.`prefectures`
SET `country_id` = 2,
`updated_at` = '2012-05-30 08:31:59'
WHERE `messages_staging`.`prefectures`.`id` = 2
SELECT `messages_staging`.`prefectures`.*
FROM `messages_staging`.`prefectures`
WHERE `messages_staging`.`prefectures`.`country_id` = 2
Country.testname
SELECT
`messages_development`.`countries`.*
FROM `messages_development`.`countries`
INNER JOIN `messages_staging`.`prefectures`
ON `messages_staging`.`prefectures`.`country_id` = `messages_development`.`countries`.`id
0 件のコメント:
コメントを投稿