0

I know this must have existed and I did follow some answers that seems to be a duplicate question, so, I followed https://stackoverflow.com/a/5858236/5614748, but am having a little issue.

**Am trying to output the highest occurring email address from a table and show it in the view.

This is what I have done:

show action

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

instance variable @highest_occurrence

highest occurrence

my output

enter image description here

Afolabi Olaoluwa
  • 1,898
  • 3
  • 16
  • 37
  • Hi @Afolabi, I'd recommend you to check out this answer if you haven't come across it already: https://stackoverflow.com/a/43481898/1611339 – timbillstrom Nov 20 '18 at 12:21
  • Don’t include screenshots of plaintext. You clearly have access to the data: copy and paste it into your post. – anothermh Nov 20 '18 at 17:06

1 Answers1

1

Please check following if necessary,

This will provide you Hash[email,objects(with same emails)]

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

Get highest repeated email id,

@summary = {
  highest_occurrence_account: @highest_occurrence.max_by { |k,v| v.count }[0]
}
ray
  • 5,454
  • 1
  • 18
  • 40
  • but there is only one problem to this: If there are fields that has no email i.e. empty, it wont show the highest occurred email. Meaning if the *nil* fields are plenty and that turn out to be the highest occurred, then the output will be nil (no value) – Afolabi Olaoluwa Nov 20 '18 at 21:39
  • @AfolabiOlaoluwaAkinwumi I considered case of email to be mandatory field, If empty email field case is present, you can filter them out in `@history_assets` or later in `@highest_occurrence` – ray Nov 21 '18 at 06:45