Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After upgrading to Rails 6.1.3 acts_as_tenants has wrong behaviour #258

Open
pinkfloydsito opened this issue Mar 29, 2021 · 3 comments
Open

Comments

@pinkfloydsito
Copy link

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
Copy link

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
Copy link
Author

pinkfloydsito commented Mar 29, 2021

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

@snawar92
Copy link

snawar92 commented Mar 1, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants