简体   繁体   中英

How to group by in Rails use Postgresql

I have a model named Product , it's attributes name , quantity

I tried to use ActiveRecord to group by name in postgresql database and it was not working

This is errors:

  Product Load (53.6ms)  SELECT "".* FROM "products" GROUP BY name
PG::GroupingError: ERROR:  column "products.id" must appear in the GROUP BY clause or be used in an aggregate function

LINE 1: SELECT "products".* FROM "products" GROUP BY name
           ^
=> #<Product::ActiveRecord_Relation:0x4d26424>

Thank you for any answer!

I would like to lead you to http://guides.rubyonrails.org/active_record_querying.html#group

Here is a example For example, if you want to find a collection of the dates orders were created on:

  Product.select('name').group("name")
  Product.select('name, sku').group("name, sku")

Note you need a select

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM