Ayman Salah
Ayman Salah

Reputation: 1089

Sort by a column in a has one relation in Rails

I have a Model (Activity) with a has_one relation to another Model (OverallRating) which has a column called overall I want to sort the Activities based on the overall column. I have searched about this but was unlucky and found very specific stuff that aren't really as simple as my question here.

Upvotes: 0

Views: 214

Answers (2)

Ayman Salah
Ayman Salah

Reputation: 1089

Thanks a lot to BroiSatse he gave me a headstart. However this is what worked for me.

Activity.joins(:overall_rating).select("activities.*").order('overall')

Upvotes: 0

BroiSatse
BroiSatse

Reputation: 44675

That will do for has_one:

Activities.joins(:overall_rating).order('overall_ratings.overall').uniq

I am pretty sure this will also work for has_many association (just add an s to the symbol in joins method) however has not tested it yet.

Upvotes: 1

Related Questions