带连接的 Pundit 策略
Pundit Policies with Joins
我有使用歌曲的教堂。
通过特定的歌曲 ID,我试图获取最近的使用日期和用户所属教会限制的使用总数。
@usages = Usage.select("MAX(services.date) as date", :song_name, :song_id, "count(song_id) as count_song_id").joins(:service, :song).where(:services => {church_id: current_user.church_id}).group(:song_name, :song_id).order("count_song_id DESC")
上面的代码似乎可以工作,但我现在已经开始实施 Pundit 授权并且 运行 遇到了一些困难。我的范围政策很简单:
class Scope < Scope
def resolve
if user.admin?
scope.all
else
scope.where church_id: current_user.church_id
end
end
end
问题是如何将它实际用于联接。这似乎不对,但我有点不知所措:
@usages = policy_scope Usage.select("MAX(services.date) as date", :song_name, :song_id, "count(song_id) as count_song_id").joins(:service, :song).group(:song_name, :song_id).order("count_song_id DESC")
那么最终起作用的(我认为)是:
@usages = policy_scope(Usage.joins :service)
.select("MAX(services.date) as date", :song_name, :song_id, "count(song_id) as count_song_id")
.joins(:service, :song)
.group(:song_name, :song_id)
.order("count_song_id DESC")
关键是policy_scope(Usage.joins :service)
我有使用歌曲的教堂。
通过特定的歌曲 ID,我试图获取最近的使用日期和用户所属教会限制的使用总数。
@usages = Usage.select("MAX(services.date) as date", :song_name, :song_id, "count(song_id) as count_song_id").joins(:service, :song).where(:services => {church_id: current_user.church_id}).group(:song_name, :song_id).order("count_song_id DESC")
上面的代码似乎可以工作,但我现在已经开始实施 Pundit 授权并且 运行 遇到了一些困难。我的范围政策很简单:
class Scope < Scope
def resolve
if user.admin?
scope.all
else
scope.where church_id: current_user.church_id
end
end
end
问题是如何将它实际用于联接。这似乎不对,但我有点不知所措:
@usages = policy_scope Usage.select("MAX(services.date) as date", :song_name, :song_id, "count(song_id) as count_song_id").joins(:service, :song).group(:song_name, :song_id).order("count_song_id DESC")
那么最终起作用的(我认为)是:
@usages = policy_scope(Usage.joins :service)
.select("MAX(services.date) as date", :song_name, :song_id, "count(song_id) as count_song_id")
.joins(:service, :song)
.group(:song_name, :song_id)
.order("count_song_id DESC")
关键是policy_scope(Usage.joins :service)