gem 'acts_as_tenant'
Rails 3アプリで使用しています。
ドメインに基づいて、アプリケーション コントローラーにテナントを設定します。
set_current_tenant_by_subdomain(:tenant, :subdomain)
current_tenant を使用する必要がある workorder モデルにコードがあります。
class Workorder < ActiveRecord::Base
acts_as_tenant(:tenant)
if ActsAsTenant.current_tenant.data.present?
ActsAsTenant.current_tenant.data.each do |key, value|
ransacker key do |parent|
Arel::Nodes::InfixOperation.new('->', parent.table[:data], key)
end
end
end
私のローカル Mac では、これで問題なく動作します。しかし、Heroku にアップロードすると、次のエラーが発生します。
Sep 17 11:25:38 ndeavor-staging app/web.1: /app/app/models/workorder.rb:8:in `<class:Workorder>': undefined method `data' for nil:NilClass (NoMethodError)
つまり、ActsAsTenant.current_tenant
(Heroku では) nil です。
何故ですか?
助けてくれてありがとう!
更新1
同じモデルがstmt で問題なく使用ActsAsTenant.current_tenant
されます。where
if ActsAsTenant.current_tenant.data != nil
ActsAsTenant.current_tenant.data.each do |key, value|
ransacker key do |parent|
Arel::Nodes::InfixOperation.new('->', parent.table[:data], key)
end
end
end
def self.woclosed
where("wostatus_id = ?", ActsAsTenant.current_tenant.workorder_closed).where(:archive => false)
end
UPDATE2
コードをアプリケーションコントローラーに移動しようとしましたが、ローカルでも機能しません:
class ApplicationController < ActionController::Base
set_current_tenant_by_subdomain(:tenant, :subdomain)
if current_tenant.data.present?
current_tenant.data.each do |key, value|
ransacker key do |parent|
Arel::Nodes::InfixOperation.new('->', parent.table[:data], key)
end
end
end
しかし、私は得る:
NameError: undefined local variable or method `current_tenant' for ApplicationController:Class
更新3
URL のサブドメインに基づいてテナントを検索する別の方法を試しました。ルックアップ コードはビューでは正常に機能しますが、モデルでは機能しませんでした。
ct = Tenant.where(subdomain: request.subdomain).first
if ct.data.present?
ct.data.each do |key, value|
ransacker key do |parent|
Arel::Nodes::InfixOperation.new('->', parent.table[:data], key)
end
end
end