1

I have a instance method called entry_sum which sums a few other instance methods.

...
def game4sum
  if games[3] and games[3].winning_team_id == game4_selection
    then 1
  else 0
  end 
end

def entry_sum
  game1sum + game2sum + game3sum + game4sum
end

In my view I'm running

<% @entries.each do |entry| %>
...
<%= entry.entry_sum %> 

My question is, is it possible to then find the max value for entry_sum if there are X amount of entries in @entries? I'm looking to identify the entry with the highest value for entry_sum. This is my first app and I have a feeling that I need to restructure/visit class vs instance methods.

mcnollster
  • 537
  • 1
  • 4
  • 14

1 Answers1

1

You can use detect and check the max value. I've put a full example below. You should only need to adapt the last line to your program.

require 'ostruct'
@entries = [OpenStruct.new(:entry_sum => 1), OpenStruct.new(:entry_sum => 4), OpenStruct.new(:entry_sum => 5), OpenStruct.new(:entry_sum => 2)]
@entries.detect { |entry| entry.entry_sum == @entries.map(&:entry_sum).max }