Rails - order by joint attribute

312 Views Asked by At

I just upgraded my project to rails 4 and i'm getting an error on this line :

Group.first.event_joins.order(event: [:threshold]).reverse

This line should select the event_joins and order them by threshold. The threshold column is in the event table not in the event_joins table. I would like to like to order the event_joins by event treshold. How can I write this in rails 4 ?

The error :

ArgumentError: Direction "[:threshold]" is invalid. Valid directions are: [:asc, :desc, :ASC, :DESC, "asc", "desc", "ASC", "DESC"]

Thanks

2

There are 2 best solutions below

2
On

This should work:

Group.first.event_joins.order('events.threshold').reverse

Rails 4 supports hash arguments, eg order(model: :asc) but i have no idea how to make it work through associations.

0
On

The event_join belongs to an event.

This worked out for me :

group.event_joins.joins(:event).order("events.threshold").reverse

cheers