has_many through:
2 つの完全に異なるサーバーに存在するデータベースの関係を作成しようとしている JRuby アプリケーションがあります。異なるサーバー上のテーブル間では結合が機能しないことを理解しています。私が望むのは、結合をシミュレートして、モデルを使用する開発者がクロスサーバー結合を (as) 意識する必要がないようにすることです。
このセットアップには、さらに複雑な点がいくつかあります。
- リモート データベースは読み取り専用です
- リモート データベースのテーブル名と主キーは、Rails の命名規則に従っていません。(リモート データベースはデータ ウェアハウスです)
- あたかも a が入っているかのようにモデルを使用できるようにしたいと考えて
has_and_belongs_to_many
います。
独自のカスタムAssociationを作成することを検討しましたが、それは少し複雑で、Rails コードを読む以外にガイドや実際の出発点が見つかりません。
私が見逃しているこれを行う簡単な方法はありますか?
カスタム ActiveRecord アソシエーションを構築することは、これを行うための最良の方法ですか? もしそうなら、どこから始めればよいですか?
私のセットアップに似たコード:
config/database.yml
development:
adapter: postgresql
encoding: unicode
database: main
username: username
password: password
host: localhost
pool: 5
remote_development: # Read only
adapter: jdbcmssql
driver: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: 'jdbc:sqlserver://foo.com;databaseName=main'
username: username
password: password
app/models/account.rb
class Portfolio < ActiveRecord::Base
#has_and_belongs_to_many :dim_users, join_table: :accounts_dim_user
end
app/models/remote_model_base.rb
class RemoteModelBase
require "#{Rails.root}/lib/sqljdbc4.jar"
self.abstract_class = true
establish_connection "remote_#{Rails.env}".to_sym
after_initialize :readonly!
end
app/models/dim_user.rb
class DimUser < RemoteModelBase
self.table_name = 'DimUser'
self.primary_key = 'dwidDimUser'
#has_and_belongs_to_many :accounts, join_table: :accounts_dim_user
end
config/schema.rb
ActiveRecord::Schema.define(version: 20140925200106) do
create_table "accounts", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "accounts_dim_user", force: true, id: false do |t|
t.integer "dwidUser"
t.integer "account_id"
t.datetime "created_at"
t.datetime "updated_at"
end
# Defined in the remote database but it might look something like this
# create_table "DimUser" do |t|
# t.integer dwidUser
# # ...
# end