2

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!

Community
  • 1
  • 1
samlester
  • 335
  • 3
  • 18

2 Answers2

3

Try

FeaturedEvent.includes(:event).order('events.start_datetime').limit(5)

In your order the table name should be the real database table name.

As I guess, the table name must be events

Krishna Rani Sahoo
  • 1,539
  • 1
  • 14
  • 25
  • @krishna sahoo : Shouldn't **include** be **includes**? Can you look at this [link](http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html) and when I am using **references** in (postgres db) method then I am getting undefined method `references ActiveRecord::Relation` for the query 'asdsdfsdf' – zeal Aug 15 '14 at 13:36
  • @krishna sahoo : Shouldn't **include** be **includes**? Can you look at this [link](http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html) and when I am using **references** in (postgres db) method then I am getting undefined method `references ActiveRecord::Relation` for the query `@messages = Message.includes(:sender).where("UPPER(body) like UPPER(?) or UPPER(body) like UPPER(?) or UPPER(sender.first_name) like UPPER(?)", "%#{"sus"}%", "%#{"ang"}%", "%#{"bar"}%").references(:people)`. – zeal Aug 15 '14 at 13:48
  • @zeal, Yes it should be `includes` – Krishna Rani Sahoo Aug 18 '14 at 08:35
1

Your table name is events so you just have to add s in event

FeaturedEvent.joins(:event).order('events.start_datetime').limit(5)
Ahmad Hussain
  • 2,443
  • 20
  • 27