有趣的問題。爲了我自己的參考,讓我試着在這個問題的上下文中總結Rails Recipes書中提出的解決方案。
1)首先加入數據庫。陽明海運
permissions:
adapter: mysql
database: permissions
username: root
password:
socket: /tmp/mysql.sock
2)請權限模型調用外部數據庫
class Permission < ActiveRecord::Base
establish_connection :permissions
end
3)創建(遷移)一權限參考表與權限編號列
4)使用PermissionReference模型作爲鏈接到權限型號
class PermissionReference < ActiveRecord::Base
belongs_to :permission
has_and_belongs_to_many :companies,
:join_table => 'companies_permissions',
:foreign_key => 'permission_id'
end
5)最後聯營公司許可
class Company < ActiveRecord::Base
has_and_belongs_to_many :permissions,
:class_name => 'PermissionReference',
:join_table => 'companies_permissions',
:association_foreign_key => 'permission_id'
end
你可能想通過繼承模型調用外部數據庫考慮重構
class External < ActiveRecord::Base
self.abstract_class = true
establish_connection :permissions
end
class Permission < External
end
我拿起這個答案過的原因下面更完整的答案是因爲下面的答案,你仍然需要做一些同步。用這種方法,你只需要告訴ruby表名。 – Steropes 2008-10-10 12:31:27