PG::Error in GROUP BY clause

后端 未结 2 1688
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 23:51

I couldn\'t think of a better way to refactor the below code (see this question), though I know it\'s very ugly. However, it\'s throwing a Postgres error (not with SQLite):<

2条回答
  •  遥遥无期
    2020-12-04 00:37

    Craig's answer explains the issue well. Active Record will select * by default, but you can override it easily:

    @user.articles.select("publication, sum(twitter_count) as twitter_count").group(:publication).each do |row|
      p row.publication # "BBC"
      p row.twitter_count # 45
    end
    

提交回复
热议问题