Rails: select unique values from a column

后端 未结 13 1907
甜味超标
甜味超标 2020-11-27 09:38

I already have a working solution, but I would really like to know why this doesn\'t work:

ratings = Model.select(:rating).uniq
ratings.each { |r| puts r.rat         


        
13条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 09:40

    You can use the following Gem: active_record_distinct_on

    Model.distinct_on(:rating)
    

    Yields the following query:

    SELECT DISTINCT ON ( "models"."rating" ) "models".* FROM "models"
    

提交回复
热议问题