如何从哈希输出最高出现的电子邮件地址

时间:2018-11-20 11:50:16

标签: ruby-on-rails ruby ruby-on-rails-4

我知道这一定存在,并且确实遵循了一些似乎是重复性问题的答案,因此,我遵循了https://stackoverflow.com/a/5858236/5614748,但有一个小问题。

** Am试图从表中输出出现率最高的电子邮件地址,并将其显示在视图中。

这就是我所做的:

显示操作

module Admin
  module Statistic
    class TrafficsController < BaseController
      def show
        @signup_grid = ::Statistic::TrafficsGrid.new(params[:statistic_traffics_grid])
        @history_assets = @signup_grid.assets
        @highest_occurrence = Hash[@history_assets.group_by {|x| x}.map {|k,v| [k.email,v.count]}]

        @summary = {
            :highest_occurrence_account => @highest_occurrence # this is my output and I have uploaded the hash it outputs instead of the email alone.
        }
        @traffic_filter = true
      end
    end
  end
end

实例变量@highest_occurrence

highest occurrence

我的输出

enter image description here

1 个答案:

答案 0 :(得分:1)

请根据需要检查以下内容,

这将为您提供Hash [电子邮件,对象(带有相同电子邮件)]

@highest_occurrence = @history_assets.group_by {|x| x.email }

获得最高的重复电子邮件ID,

@summary = {
  highest_occurrence_account: @highest_occurrence.max_by { |k,v| v.count }[0]
}