multi-tenant-support
multi-tenant-support copied to clipboard
Facing some issues on MultiTenantSupport::MissingTenantError when we add a default scope in application record
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
Ahh, I haven't cover this circumstance maybe. I'll take a look later today.
@hoppergee we have something of a similar need as well. Any help on this is most appreciated
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.