Search code examples
sqlruby-on-railsruby-on-rails-4pg

Rails4 order method does not work in PostgreSQL


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?


Solution

  • Try this:

    joins(:footprints).group(:article_id).select('SUM(articles.id) as total_articles').order('total_articles DESC')