I'm trying to sort a list of items based on the value of field on a joined table. Here's what I've got so far:
FeaturedEvent.joins(:event).order('event.start_datetime').limit(5)
This looks right to me but when it's run it returns a Postgres error about there being a missing FROM statement:
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "event" LINE 1: ...ts"."id" = "featured_events"."event_id" ORDER BY event.star... ^ : SELECT "featured_events".* FROM "featured_events" INNER JOIN "events" ON "events"."id" = "featured_events"."event_id" ORDER BY event.start_datetime LIMIT 5
I tried the suggestions in this post about putting the ordering in default scope but it came out with the same error – I'm guessing it's a Postgres thing.
How can I fix this?
Thanks!