jeudi 29 juin 2017

Sort users based on their maximum number of followers in Rails

I have 2 tables one is for users(events in my case) and follows which is a join table to users and events, I want to sort my events based on maximum no of followers they have like i can get no of followers of a particular event using

@event.followers.count

but how can I execute a query to get an event with maximum no of followers

something like this =>

@mainevent = Event.includes(:follow).where(start: Time.now-1.days .. Time.now+1.days).order(' *followers_count* ').first

or

@mainevent = Event.find_by_sql( 'followers max using sql query' )

anything to give me event ordered by max no of followers

Aucun commentaire:

Enregistrer un commentaire