Skip to content

Overwrite default list orders

Andreas Dausenau edited this page Jul 8, 2022 · 1 revision

Overwrite default list orders

The EzOnRails::ResourceController and EzOnRails::Api::ResourceController use a protected method default_order to get the default attribute and direction the list results should be ordered by. The default is { id: :asc }. You can override this method to change the default order of any default list action (search and index).

class ArticlesController < EzOnRails::ResourceController 
  ...
  
  protected

  def default_order
    { name: :desc }
  end
end

class Api::ArticlesController < EzOnRails::Api::ResourceController 
  ...
  
  protected

  def default_order
    { name: :desc }
  end
end

As you can see, the result is expected to be a hash with the attribute as key and the direction as value. Note that if you pass multiple orders, they are used for the index action, but only the first one is used by the search action.

Clone this wiki locally