Fully stuck on Ransack sorting with Associations. Want to sort the list of products from different brands.
Brands Model:
class Brand < ApplicationRecord
has_many :products
end
Brand Controller:
def show
@brand = Brand.find(params[:id])
@q = @brand.products.ransack(params[:q])
end
Brand View:
<%= search_form_for @q, url: products_path(params[:id]) do |f| %>
<%= f.search_field :brand_products_title_cont %>
<%= sort_link(@q, :brand_products_id)%>
<%= sort_link @q, :brand_products_title %>
<% end %>
I can filter on the product.title without problems, but sorting does not work. What is my mistake in thinking?
Here is the SQL output after clicking on the sorting by title:
Brand Load (0.3ms) SELECT `brands`.* FROM `brands` WHERE `brands`.`id` = 2 LIMIT 1
CACHE (0.0ms) SELECT `brands`.* FROM `brands` WHERE `brands`.`id` = 2 LIMIT 1 [["id", 2], ["LIMIT", 1]]
Rendering brands/show.html.erb within layouts/application
Product Load (19.5ms) SELECT `products`.* FROM `products` WHERE `products`.`brand_id` = ‚BRANDNAME'