GroupingError: ERROR: column must appear in the GROUP BY clause or be used in an aggregate function

前端 未结 2 1737
情话喂你
情话喂你 2020-11-29 04:15

I have code in my controller that is ranking albums by the highest average review rating (used code from this solution How to display highest rated albums through a has_many

2条回答
  •  情深已故
    2020-11-29 04:34

    Just would like to share this code on ruby using active record (sinatra)

    I had to add "group by" to an "order by" function, so line of code ...

    from:

    @models = Port.all.order('number asc')
    

    to:

    @models = Port.select(:id, :device_id, :number, :value, :sensor, :UOM).all.order('number asc').group(:id,:sensor,:UOM)
    

    and it worked perfect, just remember the ID field in this case "Port.id" must be added to the group clause otherwise will raise this error, and as @slash mentioned you can not achieve this with special functions (select implicitly through the wildcard * or in my case using "all")

提交回复
热议问题