Ruby-on-Rails: Selecting distinct values from the model

前端 未结 4 1423
攒了一身酷
攒了一身酷 2020-12-16 13:12

The docs: http://guides.rubyonrails.org/active_record_querying.html#selecting-specific-fields

Clearly state that:

query = Client.select(:name).distin         


        
4条回答
  •  旧时难觅i
    2020-12-16 13:36

    There are some approaches:

    1. Rails way:

      Model.select(:name).distinct
      
    2. Semi-rails way

      Model.select("DISTINCT ON(models.name) models.*")
      

      The second allows you to select the first record uniqued by name, but in the whole matter, not only names.

提交回复
热议问题