Search code examples
ruby-on-railssortingcountransack

Ransack sort on .count result of an has_many relation, rails 4


I want to sort a table (with a sort_link I suppose) or filter the result of a .count method. I have a model where a Candidate has_many Assets

I built a table that lists all my candidates where a column show the result of candidate.assets.count so I know how many assets a candidate have.

I want to add a filter (I could be a value from 0 to 2 for example) or a sort link generated by ransack to sort the column ascending or descending.

I hope I was clear about my issue. I checked the doc of ransack and ransacker but I couldn't find a way to do it.

Thanks you for helping me. cheers.


Solution

  • I found out using a counter cache field and sort the field with ransack