Im using gem 'acts_as_tenant'
in a Rails 3 app.
I set the tenant in the applications controller based on domain:
set_current_tenant_by_subdomain(:tenant, :subdomain)
I have code in the workorder model that needs to use the current_tenant:
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
On my local Mac, this works fine. But, when I upload to Heroku, I get this error:
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)
So, ActsAsTenant.current_tenant
is nil (on Heroku).
Why is that?
Thanks for the help!
UPDATE1
The same model uses ActsAsTenant.current_tenant
in a where
stmt with no problem:
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
I tried moving the code to the applications controller, but that doesn't even work locally:
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
But I get:
NameError: undefined local variable or method `current_tenant' for ApplicationController:Class
UPDATE3
I tried another approach by looking up the Tenant based on the URL's subdomain. The lookup code works ok in a view, but it didn't in the model.
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
I still don't know why ActsAsTenant.current_tenant
is nil
on Heroku.
But, the following fixed my issue:
class Workorder < ActiveRecord::Base
acts_as_tenant(:tenant)
ct = Tenant.find(self.first.tenant)
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