lundi 8 janvier 2018

Rails 5 - How to sort has_many associations list?

In rails 5 and in postgresql, I want to sort a list based on created_at: :desc.

Model structure is like,

Review model has_many :comments and has_many :commented_users, :through=>:comments

In views, I want to display list of users based on created_at (DESC) date and this field is from comments table

Controller query is like,

review = Review.find_by_id(params[:id])
comments = review.commented_users

If I give order for review.commented_users then obviously it will sort User, how can I sort users based on Comment?

Please help me to solve this sorting issue and I am new to postgresql

Aucun commentaire:

Enregistrer un commentaire