HUSTEN
HUSTEN

Reputation: 5197

How to sort by the column associated

I do have these tables

users and profiles
User has one profile.

There is a column called point in profiles table.

I'd like to fetch all the users sorted by the point. How can I customize this controller??

controller

@users = User.confirmed.page(params[:page]).order("point DESC")

model

scope :confirmed, where("confirmation_token" => nil)

Upvotes: 0

Views: 31

Answers (1)

house9
house9

Reputation: 20614

you have to join if you want to sort on the column using sql

@users = User.confirmed
  .page(params[:page])
  .order("point DESC")
  .joins(:profile)

Upvotes: 1

Related Questions