0

i created a method like below

def sort_by_footprints
  joins(:footprints).group(:article_id).order('SUM(articles.id) DESC')
end

but this doesn't work when i use postgresql and it says ActionView::Template::Error (PG::GroupingError: ERROR: column "articles.id" must appear in the GROUP BY clause or be used in an aggregate function how can i fix it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Fumiya
  • 127
  • 10

1 Answers1

2

Try this:

joins(:footprints).group(:article_id).select('SUM(articles.id) as total_articles').order('total_articles DESC')
Nathan
  • 7,627
  • 11
  • 46
  • 80