1

Beginner here. Have a custom class method that calculates the win probability of team_a and team_b if they were to play each other. In my view, I want to display those calculations. Trying to put this together with my limited understanding of how it works.

# team.rb
class Team
  attr_reader :home_team_win_probability, :away_team_win_probability
  def self.matchup(home_team, away_team)
    home_team_win_probability = (home_team.rating - home_team.rating * away_team.rating) / (home_team.rating) + away_team.rating - 2 * home_team.rating * away_team.rating)
    away_team_win_probability = 1 - home_team_win_probability
  end
end

# teams_controller.rb
class TeamsController < ApplicationController
  def matchups
    @team_a = Team.find_by(rank: 2)
    @team_b = Team.find_by(rank: 1)
    @matchup = Team.matchup(@team_a, @team_b)
  end
end

# matchups.html.erb
<h4>Matchup: <%= @team_a.name %> @ <%= @team_a.name %></h4>
# want to do something like this: 
<h5>Home Team Win Probability: <%= @matchup.home_team_win_probability %> </h5>
<h5>Away Team Win Probability: <%= @matchup.away_team_win_probability %> </h5>

@matchup.home_team_win_probability yields error because @matchup just returns the last line (the away team probability in this case) @matchup.home_team_win_probability => undefined method home_team_win_probability for #<BigDecimal:0x007f860872b3f8

How do I call home_team_win_probability and away_team_win_probability as calculated in my custom class method? Thanks.

2 Answers 2

1

Return a hash instead:

def self.matchup(home_team, away_team)
    home_team_win_probability = (home_team.rating - home_team.rating * away_team.rating) / (home_team.rating) + away_team.rating - 2 * home_team.rating * away_team.rating)
    away_team_win_probability = 1 - home_team_win_probability
    {home: home_team_win_probability, away: away_team_win_probability}
end

Then access as:

@matchup[:home]
@matchup[:away]
Sign up to request clarification or add additional context in comments.

1 Comment

This worked really well and is very simple. While it may make sense to move my matchup logic elsewhere, this was a very convenient answer given what I already have. Thank you!
1

There's no particular reason the matchup logic needs to be in the Team model. I would create a new model...

class Matchup
  attr_accessor :home_team, :away_team
  def initialize(home_team, away_team)
    @home_team = home_team
    @away_team = away_team
  end
  def home_team_win_probability
    (home_team.rating - home_team.rating * away_team.rating) / (home_team.rating) + away_team.rating - 2 * home_team.rating * away_team.rating)
  end
  def away_team_win_probability 
    1 - home_team_win_probability
  end
end

Now you can do this in your TeamsController...

@matchup = Matchup.new(@team_a, @team_b)

This gives you the methods @matchup.home_team_probability and @matchup.away_team_probability

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.