PostgreSQL スキーマを使用したマルチテナント アーキテクチャ

マルチテナント アーキテクチャでは、単一のインスタンスを使用して複数のアカウントを処理します。各アカウントはテナントと呼ばれます。従来の方法または gem を使用してマルチテナント アーキテクチャを実装するには、非常に多くの異なるアプローチがあります。これらすべてを比較することで、「テナントとして機能する」Gem によるマルチテナント実装の最も簡単な方法を見つけました。 Gem の表現
gem 'acts_as_tenant'
これを gem ファイルに配置した後、バンドルを実行してインストールします。 使用法: It sets the current tenant by  using subdomain and adds the ability to scope models to a tenant. For setting current tenant place the below code in your application controller Creating Tenant Account Model
class AddcolumnsToAccounts < ActiveRecord::Migration def up add_column :accounts, :sub_domain, :string end end
set_current_tenant_by_subdomain(:アカウント, :サブドメイン) 現在のテナントを手動で設定する方法
class ApplicationController < ActionController::Base set_current_tenant_through_filter before_filter :find_the_current_tenant def find_the_current_tenant current_account = Account.find_by_subdomain(サブドメイン: 'サブドメイン') set_current_tenant(current_account) end end
モデルのスコープ設定
class AddAccountToColleges < ActiveRecord::Migration
def up
add_column :colleges, :account_id, :integer
add_index  :colleges, :account_id
end
end

class College < ActiveRecord::Base
acts_as_tenant(:account)
end
Acts as tenant adds a method to validate uniqueness,  validates_uniqueness_to_tenant If you need to validate for uniqueness, you can do by using following code:
validates_uniqueness_to_tenant :title
さらに、デフォルトの Rails validates_uniqueness_of も利用できます。外部キーを指定するには、次の構文を使用します。
act_as_tenant(:account, :foreign_key => 'アカウントID')
デフォルトでは、account_id が使用されます 構成 ActsAsTenant のオプションを制御するイニシャライザを作成できます。 config/initializers/acts_as_tenant.rb で構成オプションを変更できます。
ActsAsTenant.configure は |config| を実行します。 config.require_tenant = false 終了
config.require_tenant を true に設定すると、テナントが設定されていないクエリが実行されるたびに ActsAsTenant::NoTenant エラーが発生します。
出典: https://github.com/ErwinM/acts_as_tenant
 

最新のアップデートを購読する

関連記事

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

jaJapanese