【问题标题】:Grouping games into seasons by year按年份将游戏分组到季节
【发布时间】:2014-01-07 03:01:10
【问题描述】:

我正在构建一个棒球统计应用程序。我无法将游戏分成季节。

一场比赛有_许多击球、投球和防守。游戏也有一个played_date 字段,我可以从中获取年份。

基本上我想做的就是能够将游戏分成季节,并在那个季节中添加列。

例如,能够做到:

<% @player.each do |player| %>
    <% player.battings.seasons.each do |battingseason| %>
        <%= battingseason.total("homeruns") %>
    <% end %>
<% end %>

所以 "battingseason.total("homeruns")" 将总结特定球员在该赛季中击出的总本垒打数。

我可以通过这个获得玩过哪些游戏的年份:

@game_seasons = @games.group_by { |g| g.played_date.beginning_of_year }

...但我不确定它有多大帮助。

从那里开始,我还需要能够将季节分为秋季和春季。例如,我目前拥有的:

def season
    if self.game.played_date.month >= 9 && self.game.played_date.month <= 12
        season = "fall"
    else
        season = "spring"
    end
end

所以我的问题是我只是不确定将所有这些逻辑放在哪里,在控制器中,在模型中?以及具体如何输出,谢谢!

【问题讨论】:

    标签: ruby-on-rails group-by sum


    【解决方案1】:

    我会创建一个Season 模型。季节模型有一栏,season。我会这样设置模型:

    class Season < ActiveRecord::Base
      has_many :games
      has_many :battings, through: :games
      has_many :pitchings, through: :games
      has_many :fieldings, through: :games
    end
    
    class Game < ActiveRecord::Base
      belongs_to :season
    end
    

    您需要将season_id 添加到games 表中,并且可以在game 模型中添加before_validate 方法:

    before_validate :find_or_create_season
    
    private
    
    def find_or_create_season
      if self.played_date.month >= 9 && self.played_date.month <= 12
        season = "fall"
      else
        season = "spring"
      end
      season_name = season + ' ' + self.played_date.year.to_s
      game_season = Season.find_or_create_by_season(season_name)
      self.season_id = game_season.id
    end
    

    我不确定您希望您的视图看起来如何,但这是一种可能性。这将按赛季为您提供一名球员的本垒打总结:

    在您的控制器中:

    def show
      @player = Player.find(params[:id])
    
      #not sure how you define if a batting was a homerun. I assumed that you have a boolean for that
      @homeruns_by_season = @player.battings.where('homerun = ?', true).group(:season).count
      #this will return a hash with season as a key and the count as the value
    
    end
    

    在你看来

    <% @homeruns_by_season.each do |season, homeruns| %>
      <td><%= season.season %></td>
      <td><%= homeruns %></td>
    <% end %>
    

    【讨论】:

    • 所以这需要创建一个新表,还是只需要在游戏表中创建一个新模型和一个新列。此外,在问题的示例中,输出完全符合我想要的内容的视图标签是什么。这是否对击球、投球和守备中的每一列进行求和?谢谢!
    • 是的,您需要一个新的seasons 表和games 表中的一个新的season_id 列。我将更新我的答案以包含一些view 代码。您是想先按球员还是按赛季总结本垒打。我的意思是你想先按球员总结本垒打,然后再按赛季或其他方式总结?
    • 假设有 2012 年和 2013 年的比赛。John Doe 在 2012 年打了 20 场比赛,他只有两场比赛打出全垒打。 2012 赛季,John Doe 打出 2 个本垒打。所以赛季,然后是一个赛季内的统计数据。
    • AbM,这真的很棒。除了我必须更正您发表意见的方式。如果你能解决这些问题,你就是一个救生员!所以 battings 有几个不同的列,它们都是整数。例如:batting.single、batting.double、batting.triple。我需要一种方式来表示seasons.each 做|season|,然后是、 等等...层次结构是player, games -> 按季节分组,然后那个季节的统计数据。对于每个赛季,在击球次数、投球次数和防守次数下都有多项统计数据需要汇总。谢谢!
    猜你喜欢
    • 2014-10-28
    • 2018-10-27
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 2020-04-22
    相关资源
    最近更新 更多