Ruby on rails 按值分组散列并获取Rails中一个组下的值计数

Ruby on rails 按值分组散列并获取Rails中一个组下的值计数,ruby-on-rails,hash,Ruby On Rails,Hash,我有一个代码,可以获取给定时间跨度的签入列表。请参阅下面的代码 from = Time.zone.now.beginning_of_month to = Time.zone.now.end_of_month customer_checkins = CustomerCheckin.where(account_id: seld.id, created_at: from..to) 然后,代码将为我提供满足给定条件的所有签入对象。我需要做的下一件事是对每个客户的签入列表进行分组。所以我有这个代码来

我有一个代码,可以获取给定时间跨度的签入列表。请参阅下面的代码

from = Time.zone.now.beginning_of_month
to   = Time.zone.now.end_of_month
customer_checkins = CustomerCheckin.where(account_id: seld.id, created_at: from..to)
然后,代码将为我提供满足给定条件的所有签入对象。我需要做的下一件事是对每个客户的签入列表进行分组。所以我有这个代码来做这件事

group_customer_id = customer_checkins.group(:customer_id).count
然后,按客户id对其进行分组将产生散列。见下面的例子

{174621=>9180262=>1180263=>1180272=>1180273=>3180274=>3180275=>4180276=>3180277=>2180278=>4180279=>4180280=>3180281=>5180282=>8}

现在,我想获得具有相同签入计数的客户数-多少客户有9个签入,5个签入,等等。根据上面的散列。我期待这样的输出:

{9=>1,8=>1,5=>1,4=>3,3=>4,2=>1,1=>3}


从散列中获取值,如:

customer_array = {174621=>9,180262=>1,180263=>1,180272=>1,180273=>3,180274=>3,180275=>4,180276=>3,180277=>2,180278=>4,180279=>4,180280=>3,180281=>5,180282=>8}.values
customer_count = Hash.new(0)

customer_array.each do |v|
  customer_count[v] += 1
end

puts customer_count
customer_array = {174621=>9,180262=>1,180263=>1,180272=>1,180273=>3,180274=>3,180275=>4,180276=>3,180277=>2,180278=>4,180279=>4,180280=>3,180281=>5,180282=>8}.values
customer_count = Hash.new(0)

customer_array.each do |v|
  customer_count[v] += 1
end

puts customer_count
a = {174621=>9,180262=>1,180263=>1,180272=>1,180273=>3,180274=>3,180275=>4,180276=>3,180277=>2,180278=>4,180279=>4,180280=>3,180281=>5,180282=>8}

result = a.map{|k,v| v}.each_with_object(Hash.new(0)) { |word,counts| counts[word] += 1 }

# => {9=>1, 1=>3, 3=>4, 4=>3, 2=>1, 5=>1, 8=>1}