簡體   English   中英

HABTM導軌關系中的專家范圍

[英]Pundit scope in HABTM rails relation

我當前的Index操作如下所示:

  def index
    @proposals = current_user.proposals
  end

但我想這樣做:

  def index
    @proposals = policy_scope(Proposal)
  end

我在UserProposal之間具有has_and_belongs_to關系。

我開始在我的應用程序中使用Pundit gem,但是我不知道如何定義范圍才能使普通用戶具有上述行為。

我想做這樣的事情:

  class Scope < Scope
    def resolve
      if user.admin?
        scope.all
      else
        user.proposals # HOW DO I DO THIS WITH THE SCOPE?
      end
    end
  end

如何使用scope變量獲取user.proposals 我知道,如果我具有has_manybelongs_to關系,則可以執行以下操作:

      else
        scope.where(user_id: user.id) # RIGHT?
      end

但是對於HABTM,我不知道該怎么做。

有什么幫助嗎?

您可以使用joins來獲取與用戶相關的投標。 像這樣:

def resolve
  if user.admin?
    scope.all
  else
    scope.joins(:users).where(proposals_users: { user_id: user.id })
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM