Skip to content

Facing some issues on MultiTenantSupport::MissingTenantError when we add a default scope in application record #22

@snawar92

Description

@snawar92

When adding a default scope in the ApplicationRecord of a multi-tenant Rails application with the MultiTenantSupport gem, the application throws a MultiTenantSupport::MissingTenantError. @hoppergee

is this possible I can run this default scope after set_default_scope_under_current_tenant, I tested my code by putting after set_default_scope_under_current_tenant in each model separately, it works but when I do in application record I face MissingTenantError

module RoleFilterable
  extend ActiveSupport::Concern

  included do
    default_scope { role_filtered_scope(CurrentSession&.user) }
  end

  class_methods do
    def role_filtered_scope(user)
        if roles_association_exists?(:has_and_belongs_to_many, :roles)
          joins(:roles).where(roles: { id: user.roles_ids }).distinct
        end
    end

    def roles_association_exists?(relation_type, table)
      self.reflect_on_all_associations.each do |association|
        if association.macro == relation_type && association.name == table
          return true
        end
      end
      return false
    end
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions