0

I have implemented Searchkick in a rails app and it is working fine. I want to order my results based on the number of associated objects. For Eg. If I am searching for users by their name, then I want to order the results by the number of followers. (User having most followers should come first)

@users = User.search "2% #{query}", include: [:followers], fields: [:name]

Thanks for your help in advance

Siddhant
  • 293
  • 1
  • 3
  • 8

1 Answers1

0

I think you need to add counter cache (http://guides.rubyonrails.org/association_basics.html 4.1.2.3) and use followers_count column on User model to order it.

User.search "2% #{query}", include: [:followers],
            fields: [:name], order: followers_count
Juanito Fatas
  • 9,419
  • 9
  • 46
  • 70