multi-tenant-support icon indicating copy to clipboard operation
multi-tenant-support copied to clipboard

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

Open snawar92 opened this issue 1 year ago • 3 comments

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

snawar92 avatar May 02 '24 14:05 snawar92

Ahh, I haven't cover this circumstance maybe. I'll take a look later today.

hoppergee avatar May 03 '24 04:05 hoppergee

@hoppergee we have something of a similar need as well. Any help on this is most appreciated

bazooka720 avatar May 03 '24 13:05 bazooka720

Hi @snawar92 , could you help to create a demo app for me to try? I tired to add a simple default_scope in application record, it seems work.

hoppergee avatar May 05 '24 06:05 hoppergee