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

An event has a column popularityand many keywords. The keyword has a category and a name. I try to sort events by their popularity, but then I return the most popular event from each keyword with the category "taxonomy".

Here is my request:

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

But I get below the error:

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

Where am I going wrong?

+4
source share
1 answer
Event.order(:popularity)
     .joins(:keywords)
     .group('events.id') # <======
     .where(keywords: { category: 'taxonomy' })
     .group('keywords.name')
+6
source

Source: https://habr.com/ru/post/1658125/


All Articles