問題多租戶應用程序中的acts_as_list - 如何設置範圍?
我在多租戶應用程序中使用acts_as_list。我使用Railscast模擬了多租戶:#388 Multitenancy with Scopes(需要訂閱)。
我需要確保我的around_filter工作,以便租戶不會影響其他租戶的數據。
什麼是範圍acts_as_list強制執行多租戶的最佳方式,因爲acts_as_list忽略了default_scope?
背景
的基本思想是使用around_filter在application_controller.rb這樣的:
class ApplicationController < ActionController::Base
include SessionsHelper
around_filter :update_current_tenant
#current_tenant is defined in sessions_helper.rb, included above
def update_current_tenant
Tenant.current_id = current_tenant.id if current_tenant
end
end
這樣,每一個頁面的應用程序加載時,current_tenant是設置,我可以使用default_scope將訪問權限制爲current_tenant的唯一內容。就像這樣:
default_scope { where(tenant_id: Tenant.current_id) }
但acts_as_list使用Model.unscoped(Submission.unscoped,在這種情況下),繞過我的default_scope。
這裏是我工作的具體型號:
class Submission < ActiveRecord::Base
default_scope { where(tenant_id: Tenant.current_id) }
acts_as_list scope: [:for_date, :tenant_id] # Works, but is this safe?
# The one below is closer to what I want, but it bombs
#acts_as_list :scope => 'for_date = #{for_date} AND tenant_id = #{Tenant.current_id}'
end
我在其他一些地方做到這一點,但我用一個ID列,而不是for_date,所以acts_as_list聲明如下:
acts_as_list :scope => 'task_id = #{task_id} AND tenant_id = #{Tenant.current_id}'
這很好。但是,當我嘗試這與我提交模型,包括在acts_as_list範圍for_date,我得到這個錯誤:
PG::UndefinedFunction: ERROR: operator does not exist: date = integer
LINE 1: ... (submissions.position IS NOT NULL) AND (for_date = 2013-11-...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "submissions".* FROM "submissions" WHERE (submissions.position IS NOT NULL) AND (for_date = 2013-11-25 AND tenant_id = 1) ORDER BY submissions.position DESC LIMIT 1
我將如何添加這樣一個明確的類型轉換?還是有更好的方法來做到這一點?