curved-father-82134
08/01/2023, 12:45 PMActiveRecord::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.loud-jewelry-99127
08/01/2023, 1:03 PMloud-jewelry-99127
08/01/2023, 1:05 PMloud-jewelry-99127
08/01/2023, 1:06 PMcurved-father-82134
08/01/2023, 1:28 PMcurved-father-82134
08/01/2023, 1:32 PM<%= 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.curved-father-82134
08/01/2023, 1:35 PM@sort_by = params[:sort_by]
if params[:sort_by] == 'old_to_new'
Order.order(created_at: :desc)
else
Order.order(created_at: :asc)
end
loud-jewelry-99127
08/01/2023, 1:35 PMbase_controller
already useloud-jewelry-99127
08/01/2023, 1:35 PMresolve_query_scope
(forget about this one, params are not accesible here)curved-father-82134
08/01/2023, 1:36 PMcurved-father-82134
08/01/2023, 1:37 PMcurved-father-82134
08/01/2023, 1:39 PMif params[:sort_by] == 'name'
Order.order(name: :desc)
else
Order.order(name: :asc)
end
loud-jewelry-99127
08/01/2023, 1:44 PMsort_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...
errorloud-jewelry-99127
08/01/2023, 1:44 PMcurved-father-82134
08/01/2023, 1:44 PMloud-jewelry-99127
08/01/2023, 1:45 PMloud-jewelry-99127
08/01/2023, 1:46 PMruby
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
curved-father-82134
08/01/2023, 1:46 PMloud-jewelry-99127
08/01/2023, 1:47 PMcurved-father-82134
08/01/2023, 1:48 PMcurved-father-82134
08/01/2023, 1:58 PM?sort_by=old_to_new
and this is the index action logic.
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... ^
loud-jewelry-99127
08/01/2023, 1:59 PMsort_by
loud-jewelry-99127
08/01/2023, 1:59 PMsort_by
expecting certain behaviors, not expecting the one that you're implementingcurved-father-82134
08/01/2023, 2:39 PM@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
loud-jewelry-99127
08/01/2023, 2:40 PM@some_params
have the expected value?curved-father-82134
08/01/2023, 2:42 PM@some_params
name_z_to_aloud-jewelry-99127
08/01/2023, 2:45 PMloud-jewelry-99127
08/01/2023, 2:45 PMcurved-father-82134
08/01/2023, 2:46 PMloud-jewelry-99127
08/01/2023, 2:46 PMruby
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
curved-father-82134
08/01/2023, 2:55 PMloud-jewelry-99127
08/01/2023, 2:55 PMloud-jewelry-99127
08/01/2023, 2:55 PM