title: Scope for nested resources using pundit resolve method
body: <p>I am referring to my own question <a href="https://stackoverflow.com/questions/41986700/rails-nested-resources-with-pundit-allowing-index">Rails Nested Resources with Pundit Allowing Index</a> and finally came up with a working solution but is there not any much better solution defining scope.where(?) or scope.select(?) in the property_policy? How to get all the properties that only belongs to one specific deal using the pundit resolve method?</p>
  
  <p>What I finally did :</p>
  
  <p>properties_controller.rb</p>
  
  <pre><code>class PropertiesController &lt; ApplicationController
  before_action :set_deal, except: [:index, :all]
  before_action :set_property, only: [:show, :edit, :update, :destroy]
  
  def all
    @properties = Property.all
    authorize @properties
  end
  
  def index
    @deal = Deal.find(params[:deal_id])
    @properties = policy_scope(Deal)
  end
  
  def set_deal
    @deal = Deal.find(params[:deal_id])
    # pundit ######
    authorize @deal
    ###############
  end
  (...)
  end
  </code></pre>
  
  <p>property_policy.rb</p>
  
  <pre><code>class PropertyPolicy &lt; ApplicationPolicy
  class Scope &lt; Scope
  def resolve
    scope.all if user.admin?
  end
  def all?
    user_is_admin?
  end
  def user_is_admin?
    user.try(:admin?)
  end 
  (...)
  end
  </code></pre>
  
  <p>What I'd like better:</p>
  
  <p>properties_controller.rb</p>
  
  <pre><code>def index
    @deal = Deal.find(params[:deal_id])
    @properties = policy_scope(Property) # =&gt; for # @properties = @deal.properties
    authorize @deal
  end
  </code></pre>
  
  <p>and in the property_policy.rb something like</p>
  
  <pre><code>def resolve
    # scope.where(???) if user.admin? # only an admin user can see the @deal.properties
    # or any other solution using scope
   end
  </code></pre>
  
  <p>As a reminder 1 deal has many properties and 1 property belongs to one specific deal. My routes are nested deals/id/properties except for the full list of properties I have simple "/properties". Thanks a lot for helping.</p>
  
  <p>** UPDATE **</p>
  
  <p>I finally went for </p>
  
  <p>properties_controller.rb</p>
  
  <pre><code>def index
    @deal = Deal.find(params[:deal_id])
    @properties = policy_scope(@deal.properties)
    authorize @properties, :index?
  end
  </code></pre>
  
  <p>and in property_policy.rb</p>
  
  <pre><code>class PropertyPolicy &lt; ApplicationPolicy
    class Scope &lt; Scope
      def resolve
        user.admin? ? scope.all : scope.none
      end
    end
    def index?
      user_is_admin?
    end
    def user_is_admin?
      user.try(:admin?)
    end
  end
  </code></pre>
  
  <p>Not sure if it is the proper way</p>