ErwinM / acts_as_tenant

Easy multi-tenancy for Rails in a shared database setup.
MIT License
1.56k stars 264 forks source link

After upgrading to Rails 6.1.3 acts_as_tenants has wrong behaviour #258

Open pinkfloydsito opened 3 years ago

pinkfloydsito commented 3 years ago

Rails 6.1.3 ruby 3.0.0 acts_as_tenant devise 4.7.3 acts_as_tenant 0.5.0

I have 2 databases, the first one is used to manage some user/account related information users, accounts, country... The second one is the one that depends on the field subdomain from the account table of the first database.

When I used this gem in the version of Rails 5.2 everything is cool, but now every time that the tenant is set all the models try to validate against the tenant selected, even the ones that should not.

class ApplicationController < ActionController::Base
  before_action :authenticate_user!

  before_action :set_host
  around_action :set_current_user

  set_current_tenant_through_filter
  before_action :set_tenant

  def set_tenant
    account = Account.all_cached.select{|acc| acc.admin_url == request.host }[0]

    if account
      @all_account_domains = Domain.all_cached.select { |d| d.account_id == account.id }
      @domain_default = @all_account_domains.select{ |d| d.id == account.domain_default }[0]
      @commercial_domains = AccountDomain.all_cached.select { |ad| ad.account_id == account.id}

      has_config = Rails.cache.fetch("Config.domain-#{account.domain_default}", expires_in: 8.hours) {
        Config.exists?(domain_id: account.domain_default)
      }

      if has_config
        config = Config.where(domain_id: account.domain_default).try(:first)
        config_maintenance = config.try(:maintenance)
        config_msg = config_maintenance > 0 ? config.try(:msg) : nil
        render(file: File.join(Rails.root, 'public/503.html'), status: 503, layout: false, locals: {msg: config_msg}) unless config_msg.nil?
      end
    else
      render(file: File.join(Rails.root, 'public/403.html'), status: 403, layout: false)
    end
  end

  def set_current_user
    Current.user = current_user
    if Current.user
      Current.user.current_account = Account.all_cached.select { |acc| acc.admin_url == request.host }[0]
    end
    yield
  ensure
    Current.user = nil
  end

Right now every time current_tenant is set and I get inside the set_current_user method the table is not available

(byebug) User
User(Table doesn't exist)

This happens with sessions from Devise too image

is anyone having the same trouble?

pruzicka commented 3 years ago

I have exactly the same gems as you do (rails 6.1.3.1 now) different setup for tenant though, one DB - zero problems.

pinkfloydsito commented 3 years ago

In my case we use two schemas, until Rails 6.0.0 it worked perfectly fine, but it breaks in later versions.

snawar92 commented 2 years ago

In my case we use two schemas, until Rails 6.0.0 it worked perfectly fine, but it breaks in later versions.

@pinkfloydsito give a try to this gem I was facing same issues in latter rails versions, but this gem helped

https://github.com/hoppergee/multi-tenant-support