Rails: select unique values from a column

后端 未结 13 1837
甜味超标
甜味超标 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"
    
    0 讨论(0)
  • 2020-11-27 09:41

    Another way to collect uniq columns with sql:

    Model.group(:rating).pluck(:rating)
    
    0 讨论(0)
  • 2020-11-27 09:42

    If anyone is looking for the same with Mongoid, that is

    Model.distinct(:rating)
    
    0 讨论(0)
  • 2020-11-27 09:44
    Model.select(:rating)
    

    The result of this is a collection of Model objects. Not plain ratings. And from uniq's point of view, they are completely different. You can use this:

    Model.select(:rating).map(&:rating).uniq
    

    or this (most efficient):

    Model.uniq.pluck(:rating)
    

    Rails 5+

    Model.distinct.pluck(:rating)
    

    Update

    Apparently, as of rails 5.0.0.1, it works only on "top level" queries, like above. Doesn't work on collection proxies ("has_many" relations, for example).

    Address.distinct.pluck(:city) # => ['Moscow']
    user.addresses.distinct.pluck(:city) # => ['Moscow', 'Moscow', 'Moscow']
    

    In this case, deduplicate after the query

    user.addresses.pluck(:city).uniq # => ['Moscow']
    
    0 讨论(0)
  • 2020-11-27 09:45
    Model.select(:rating).uniq
    

    This code works as 'DISTINCT' (not as Array#uniq) since rails 3.2

    0 讨论(0)
  • 2020-11-27 09:45

    If I am going right to way then :

    Current query

    Model.select(:rating)
    

    is returning array of object and you have written query

    Model.select(:rating).uniq
    

    uniq is applied on array of object and each object have unique id. uniq is performing its job correctly because each object in array is uniq.

    There are many way to select distinct rating :

    Model.select('distinct rating').map(&:rating)
    

    or

    Model.select('distinct rating').collect(&:rating)
    

    or

    Model.select(:rating).map(&:rating).uniq
    

    or

    Model.select(:name).collect(&:rating).uniq
    

    One more thing, first and second query : find distinct data by SQL query.

    These queries will considered "london" and "london   " same means it will neglect to space, that's why it will select 'london' one time in your query result.

    Third and forth query:

    find data by SQL query and for distinct data applied ruby uniq mehtod. these queries will considered "london" and "london " different, that's why it will select 'london' and 'london ' both in your query result.

    please prefer to attached image for more understanding and have a look on "Toured / Awaiting RFP".

    enter image description here

    0 讨论(0)
提交回复
热议问题