1

I have a very strange issue with Ransack that I'm finding hard to pin down. I have a table in Solidus which displays a product listing for an administrator to search through. This product listing can be sorted by several column names (namely, sku, name, and price). However, when I try to sort by the sku column, the listing becomes out of order within the last ~30 items of the listing (there are about 3,000 products listed in total). It seems that it stops sorting the sku names after a certain point, nearly at the end. This only happens when I sort by the sku column, not name or price.

This is the code I'm using for these columns:

<thead>
  <tr data-hook="admin_products_index_headers">
    <th><%= sort_link @search, :sku, Spree::Product.human_attribute_name(:sku), { default_order: "desc" }, {title: 'admin_products_listing_sku_title'} %></th>
    <th></th>
    <th><%= sort_link @search, :name, Spree::Product.human_attribute_name(:name), { default_order: "desc" }, {title: 'admin_products_listing_name_title'} %></th>
    <th class="align-right"><%= sort_link @search,:master_default_price_amount, Spree::Product.human_attribute_name(:price), {}, {title: 'admin_products_listing_price_title'} %></th>
    <th data-hook="admin_products_index_header_actions" class="actions"></th>
  </tr>
</thead>

I really have no idea what about the sku column is causing the sorting to become incorrect after this certain point near the end. The sku strings look like this: AB123456. All of them start with the letters AB and then a unique integer which identifies the product. As far as I can tell, this should totally be something Ransack's sort_link can handle without trouble. But alas, here I am scratching my head...

GDP2
  • 1,948
  • 2
  • 22
  • 38
  • can you show sorting code? – Vishal Apr 04 '19 at 10:45
  • @Vishal I believe it is just using SQL ordering. It took me some time to find the specific file, but [see this code in the Ransack codebase.](https://github.com/activerecord-hackery/ransack/blob/v2.1.1/lib/ransack/adapters/active_record/context.rb#L35-L55) Also, [see this page on the Ransack Demo site.](https://ransack-demo.herokuapp.com/users?_method=get%3D%3D&q[s]=id+desc) It shows the SQL query that is being used under the hood: `SELECT "users".* FROM "users" ORDER BY "users"."id" DESC ` – GDP2 Apr 04 '19 at 19:39

0 Answers0