1

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'
Karsten
  • 81
  • 1
  • 13

3 Answers3

2

I solved this problem in the following way:

class Job < ApplicationRecord
 has_one :address
end

Address looks like

{ street, city, state, zipcode }

I wanted to sort on job.address.street

In my controller I have:

@q = Job.ransack(params[:query])
@q.sorts = params.dig(:q, :s) || 'id asc'

And in my view I have:

<%= sort_link(@q, :address_street) %>
Libby
  • 932
  • 8
  • 15
0

The Ransack README has this to say about sorting with associations:

If you have trouble sorting on associations, try using an SQL string with the pluralized table ('departments.title','employees.last_name') instead of the symbolized association (:department_title), :employees_last_name).

In your case, that would look like this:

sort_link @q, 'products.title'
coreyward
  • 77,547
  • 20
  • 137
  • 166
  • Already tried that before asking. Did not help. Also after the second edit from you did not change this. Sorry. How do I get the SQL query from clicking the sorting? – Karsten Jan 22 '17 at 20:16
  • @Karsten Can you share the SQL query generated? There's not enough information to figure out what's going on. – coreyward Jan 22 '17 at 20:16
  • How do I get the SQL query from clicking the sorting? It does not show up in the log. – Karsten Jan 22 '17 at 20:31
  • Do you have SQL query logging enabled otherwise? You should also be able to do `.to_sql` on the query object. – coreyward Jan 22 '17 at 20:54
0

You are using ransack for @brand.products not for @brand

In this case you can try something like this in the View:

<%= search_form_for @q, url: brand_path(params[:id]) do |f| %>
    # Search if the name field contains...
    <%= f.search_field :title_cont %>
    <%= sort_link(@q, :id)%>
    <%= sort_link @q, :title %>
<% end %>
Pavel K
  • 261
  • 2
  • 5
  • Unfortunately does not help. :( – Karsten Jan 23 '17 at 08:27
  • 1
    Could you please look at https://sq-41795404.herokuapp.com/brands/1 ? It seems to be worked for me. The code for this Heroku App is here https://github.com/loadkpi/stackoverflow-question-41795404 – Pavel K Jan 23 '17 at 09:46
  • Wow, this is amazing. Thank you for all your extra work and actually your code helps me a lot. It is still not working with the code in your answer, but your Heroku solution is working and the problem is definitely in the view. Your code in Heroku that does the trick is this: <%= @q.result.map{|p| '
  • ' + p.title + '
  • '}.join.html_safe %>
    However if I reference @q.products_title, I get the same error. – Karsten Jan 23 '17 at 16:52
  • It seems to be the "result" that was causing the problem. The code works with @q.result.. Thanks a lot for your efforts and help. Really appreciated all the help that I got here! – Karsten Jan 23 '17 at 17:06
  • Does someone know why .result makes all the difference. I´m using Rails 5. – Karsten Jan 24 '17 at 17:18