Ordering for index page
# avo-2
c
Hello! Greetings to all! @here@lemon-wall-20836 Is there any way to implement custom ordering for the index page, specifically for table records? In the resource index action, I am receiving the value '**old_to_new**' in the params by using @sort_by = params[:sort_by]. My intention is to execute the query **Order.order(created_at: :desc) **when I receive the value old_to_new in the params. However, instead of getting the desired result, I encounter the following error in the development logs:
ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column orders.old_to_new does not exist LINE 1: ...uct_id" WHERE "websites"."user_id" = $2 ORDER BY orders.old... ^
This error is expected because old_to_new is not a column in the database. I noticed that the order_by method is being called based on the sort_by=my_value&sort_direction=desc params value. Therefore, I would like to unable the sorting when the custom sort button contains the value old_to_new.
l
Hi @curved-father-82134. Can you explain the use case? You want a custom query for some resource's index page when the page loads?
I'm not sure if i understand the context. "Is there any way to implement custom ordering for the index page, specifically for table records?" We have this ordering feature that allow you to reorder records by clicking on the desired action https://docs.avohq.io/2.0/records-reordering.html#records-ordering
Im not sure if you want this kind of ordering, sort by field or a index page custom query
c
Not, that one.
I have a custom button e.g.
<%= button_to sort_option.humanize, "orders?sort_by=#{old_to_new}" %>
that I'm using. inside this file
resource_index_component.html.erb
When I click the button it goes to my resource index action. with sort_by parameter value.
In this action, I'm calling this
Copy code
@sort_by = params[:sort_by]

      if params[:sort_by] == 'old_to_new'
        Order.order(created_at: :desc)
      else 
        Order.order(created_at: :asc)
      end
l
Hmm, not sure if this will work but try to use another param name, not one that
base_controller
already use
catch that param and do the logic on the
resolve_query_scope
(forget about this one, params are not accesible here)
c
It works when I use the same value as the parameter which I have in my table.
But my scenario is different I'm using a switch case based on the params value I'm calling this Order.order(created_at: :desc) blah blah bah.
If I use it like this it will work because the name column exists in the table
Copy code
if params[:sort_by] == 'name'
        Order.order(name: :desc)
      else 
        Order.order(name: :asc)
      end
l
We use
sort_by
from params so if you pass an attribute of the table it will work, otherwise it will show you the
ActiveRecord::StatementInvalid (PG::UndefinedColumn...
error
You can use another name, that have no conflict with the index params that we already use
c
Yes, You are right.
l
and override the index method on the controller of your resource
doing something like
Copy code
ruby
def index
  super

  # your logic here
  if params[:some_param_name] == 'old_to_new'
    @query = @query.order(created_at: :desc)
  else 
    @query = @query.order(created_at: :asc)
  end 
end
c
Let me try that one. Thanks 👍
l
Sure, anytime, let me know if it works
c
Sure
unfortunately, it is not working. this is the route parameter
?sort_by=old_to_new
and this is the index action logic.
Copy code
super

      if params[:sort_by] == 'old_to_new'
        @query = @query.order(created_at: :desc)
      else 
        @query = @query.order(created_at: :asc)
      end
and getting this error
ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column orders.old_to_new does not exist LINE 1: ...uct_id" WHERE "websites"."user_id" = $2 ORDER BY orders.old... ^
l
dont use the
sort_by
we already have code using the
sort_by
expecting certain behaviors, not expecting the one that you're implementing
c
Well, I'm not facing errors anymore. but the sorting is still not working for the index page table 😔 . Here is my final logic in index action.
Copy code
@some_params = params[:some_param_name]

      case @some_params
      when 'old_to_new'
       @query = @query.order(created_at: :desc)
       puts 'old_to_new 
      when 'new_to_old'
        @query = @query.order(created_at: :asc)
      when 'name_a_to_z'
        @query = @query.order(name: :asc)
      when 'name_z_to_a'
        @query = @query.order(name: :desc)
      else
        @query = @query.order(created_at: :asc) 
      end
l
Can you confirm that
@some_params
have the expected value?
c
Here is what I'm getting from
@some_params
name_z_to_a
l
ah ok, i think i know whats happening, the records are fetched before your logic
try to do this
c
Yeah
l
Copy code
ruby
def index
  unless defined? @query
    @query = @resource.class.query_scope
  end

  case params[:some_param_name]
  when 'old_to_new'
   @query = @query.order(created_at: :desc)
  when 'new_to_old'
    @query = @query.order(created_at: :asc)
  when 'name_a_to_z'
    @query = @query.order(name: :asc)
  when 'name_z_to_a'
    @query = @query.order(name: :desc)
  else
    @query = @query.order(created_at: :asc) 
  end

  super
end
c
Thank you so much! @loud-jewelry-99127 for your valuable time 🙏 It's working 🚀
l
I'm glad that's working! Anytime!