Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

distinct for sorting with ransack

i was trying to sort values of a table with sort_link from Ransack. i found an example which is really helpful. but, i think i'm having problem with 'distinct'. i got this error 'ORDER-BY expressions must appear in SELECT list; DISTINCT'. my code looks like this :

q =user.joins(:buyer).order('users.name')
ransack = params["filter"]
@search = q.search(ransack)
@users = @search.result(:distinct=>true)

do i forget something? thanks in advance!

like image 655
n4d Avatar asked Nov 02 '22 11:11

n4d


1 Answers

As per this issue you might be able to solve this with an ActiveRecord joins query or select query to add the columns needed, for example:

q = user.order('users.name')
ransack = params["filter"]
@search = q.search(ransack).
            result(:distinct=>true).
            joins(:buyer).
            includes(:buyer)
@users = @search.all
like image 96
Mikel Lindsaar Avatar answered Nov 13 '22 16:11

Mikel Lindsaar