Rails acts like a null renter in a model on Heroku
I am using gem 'acts_as_tenant'
Rails 3 in my application.
I have set the tenant to a domain based app controller:
set_current_tenant_by_subdomain(:tenant, :subdomain)
I have some code in a work order model that needs to use 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 great. But, when I boot into 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 it ActsAsTenant.current_tenant
is nil (on Heroku).
Why is this?
Thanks for the help!
Update1
The same model ActsAsTenant.current_tenant
uses where
stmt without issue:
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 to move the code to the app 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 am getting:
NameError: undefined local variable or method `current_tenant' for ApplicationController:Class
Update3
I tried a different approach by looking at Subdomain URL renter. The search code works fine in the view, but it was not 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
source to share
I still don't know why ActsAsTenant.current_tenant
there is nil
on Heroku.
But, the following fixed my problem:
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
source to share