我有一个多租户应用程序,我正在像这样设置当前租户:
class ApplicationController < ActionController::Base
around_filter :scope_current_tenancy
def scope_current_tenancy
Tenancy.current_id = current_tenancy.id if request.subdomain != 'www'
yield
ensure
Tenancy.current_id = nil
end
end
然后在我的用户模型中,我default_scope
定义为只访问我的租户内的用户:
class Postulant < ActiveRecord::Base
default_scope ->{ where("enlistments.tenancy_id = ?", Tenancy.current_id).includes(:enlistments).references(:enlistments) }
到目前为止这有效,但现在使用devise_invitable
并尝试接受邀请我收到了一条Filter chain halted as :resource_from_invitation_token rendered or redirected
消息。问题是因为我的scope_current_tenancy
过滤器是在 之后执行的resource_from_invitation_token
,所以resource
没有正确加载。
class Devise::InvitationsController < DeviseController
prepend_before_filter :resource_from_invitation_token, :only => [:edit, :destroy]
def resource_from_invitation_token
# Here 'resource_class' is my Postulant model, so when I call
# 'find_by_invitation_token' applies the defined default_scope
# which doesn't word without 'scope_current_tenancy'
unless params[:invitation_token] && self.resource = resource_class.find_by_invitation_token(params[:invitation_token], true)
set_flash_message(:alert, :invitation_token_invalid)
redirect_to after_sign_out_path_for(resource_name)
end
end
end
所以我的问题是,有没有办法比:scope_current_tenancy
之前运行:resource_from_invitation_token
?
我试图改变around_filter :scope_current_tenancy
,prepend_around_filter :scope_current_tenancy
但我没有运气。有什么想法吗?