Ransack: Custom Scopes with groupings?

Created on 17 Mar 2016  路  3Comments  路  Source: activerecord-hackery/ransack

Suppose I have the model:

class Model < ActiveRecord::Base
    scope :test_scope, -> { where(foo: "foo", bar: "bar")}
    def self.ransackable_scopes(a = nil)
        %i( test_scope )
    end
end

the controller:

def index
  @q = Model.ransack(params[:p])
  @models = @q.result
  if @q.groupings.blank?
      @q.build_grouping
  end
end

the view:

<%=search_form_for @q do |f|%>
    <%=f.grouping_fields @q.groupings[0] do |g|%>
       <%=g.check_box :test_scope%>
    <%end%>
<%end%>

this throws:

undefined method `test_scope' for Grouping <>:Ransack::Nodes::Grouping

Is there a correct way to use groupings with scopes?

Help Wanted

Most helpful comment

Any news about this issue?

All 3 comments

Unfortunately scopes are only attached to the top level search right now. It would be nice to get it moved into the nested context, but that's going to take some work.

+1

Any news about this issue?

Was this page helpful?
0 / 5 - 0 ratings