10

An event has a column popularity and many keywords. A keyword has a category and a name. I am trying to order events by their popularity, but then only return the most popular event from each keyword name with the category "taxonomy".

Here's my query:

Event
  .order(:popularity)
  .joins(:keywords)
  .where(keywords: {category: "taxonomy"})
  .group("keywords.name")

But I am getting below error:

PG::GroupingError: ERROR: column "events.id" must appear in the GROUP BY clause or be used in an aggregate function

Where am I going wrong?

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
Jackson Cunningham
  • 4,973
  • 3
  • 30
  • 80

1 Answers1

10
Event
  .order(:popularity)
  .joins(:keywords)
  .group('events.id') # <======
  .where(keywords: { category: 'taxonomy' })
  .group('keywords.name')
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145