title: How to modify the filter on datagrid based on the current scope in rails
body: <p>This is one of my many filters:</p>
  
  <pre><code> filter(:item_id, :enum,
           :header =&gt; "Category",
           :select =&gt; lambda { scope.select(" distinct(category.title), transactions.item_id ").map {|p| [p.title, p.item_id]}},
           :multiple =&gt; false,
           :include_blank =&gt; true
    ) do |value|
        self.where(:items =&gt; { :id =&gt; value } )
    end
  </code></pre>
  
  <p>As you can see it is calling the Category from the DB (All of them)</p>
  
  <p>In my report in the SalesReportController I have this:</p>
  
  <pre><code>  def index
      user_acls = find_current_user_acls
      @grid = SalesReportsGrid.new(params[:sales_reports_grid]) do |scope|
        scope.where("access_lists.id" =&gt; user_acls).page(params[:page])
      end
      @grid.assets
  
    end
  </code></pre>
  
  <p>This will give me the scope which generate the Grid and passes to the view.</p>
  
  <p>Now I need to make the filter to show only the sold categories by this user.</p>
  
  <p>All I need to do is somehow passing the <code>user_acls</code> to the <code>SalesReportsGrid</code> Class </p>
  
  <p>If I can pass the <code>user_acls</code> then I can do this:</p>
  
  <pre><code> filter(:item_id, :enum,
           :header =&gt; "Category",
           :select =&gt; lambda { scope.select(" distinct(category.title), transactions.item_id ").where("access_lists.id" =&gt; user_acls).map {|p| [p.title, p.item_id]}},
           :multiple =&gt; false,
           :include_blank =&gt; true
    ) do |value|
        self.where(:items =&gt; { :id =&gt; value } )
    end
  </code></pre>
  
  <p><strong>A Quick explanation:</strong></p>
  
  <p>For example:</p>
  
  <p>User1 is an agent
  She sold 100 items from 4 categories.
  She goes to the report page.
  At the moment based on the above code I show her only her own sold items however the filter dropdown consists of all of the categories in the DB which it doesn't make sense to have.</p>
  
  <p>Unfortunately my ACL module is a bit granular and when I try to extend it in the <code>SalesReportsGrid</code> (<code>sales_reports_grid.rb</code>) I get undefined error for some of the methods. it is because my Authentication module has lots of other methods from other modules like Devise and Declarative Authorization. So I don't think extend will be an option here.
  Thanks in advance  </p>
  
  <p>Edit:</p>
  
  <p>in the controller:</p>
  
  <pre><code>def index
     # This will return an array of ACL ids for the user
    user_acls = find_current_user_acls 
  
    @grid = SalesReportsGrid.new(params[:sales_reports_grid].merge(user_acls: user_acls)) do |scope|
        scope.where("access_lists.id" =&gt; user_acls).page(params[:page])
    end
    @grid.assets
  end
  </code></pre>
  
  <p>beside the error I get about: <code>undefined method merge for nil:NilClass</code></p>
  
  <p><code>params[:sales_reports_grid]</code> has nothing in it that is why I get the error. </p>
  
  <p>when I use this <code>SalesReportsGrid.new(user_acls: user_acls )</code> on its own, I get the result. </p>
  
  <p>I guess this is the limit of my ActiveRecord :(</p>
  
  <p>I have added this in the SalesGrid <code>attr_accessor :user_acls</code></p>
  
  <p>Thanks</p>
  
  <p><strong>All worked mate:</strong></p>
  
  <p>Can you please explain this bit as I cant understand it: 
  When I added this:
  <code>:select =&gt; lambda { scope.select(" distinct(categories.title), transactions.item_id ").where("access_lists.id" =&gt; :user_acls).map {|p| [p.title, p.item_id]} }</code></p>
  
  <p><code>:user_acls</code> value never returned however when I do this:</p>
  
  <pre><code> def categories_select
      scope.select(" distinct(category.title), transactions.item_id ").where("access_lists.id" =&gt; user_acls).map {|p| [p.title, p.item_id]}
  
    end
  </code></pre>
  
  <p>and add the <code>:select =&gt; :categories_select</code> all works fine.</p>
  
  <p>Cheers. </p>