methyl
methyl

Reputation: 3312

has_many associations

So, my question is: how to make possible that calls:

Clan.win #to get all won rounds
Clan.blue #to get all rounds when clan was in blue team

or even this:

Clan.matches_win #need to calculate which team won match by counting rounds
Clan.matches_lost

Round model:

class Round < ActiveRecord::Base
  belongs_to :match
  has_and_belongs_to_many :banned_champions, :class_name => "Champion", :join_table => "banned_champions_rounds"
  belongs_to :clan_blue, :class_name => "Clan", :foreign_key => "clan_blue_id"
  belongs_to :clan_purple, :class_name => "Clan", :foreign_key => "clan_purple_id"
  belongs_to :winner, :class_name => "Clan", :foreign_key => "winner_id"
end

Clan model:

class Clan < ActiveRecord::Base
  has_many :players
  has_many :rounds
end

Match model:

class Match < ActiveRecord::Base
  has_many :rounds
end

Upvotes: 0

Views: 53

Answers (1)

Jakub Hampl
Jakub Hampl

Reputation: 40533

In your Clan class:

def win 
  rounds.where("winner_id = ?", self.id)
end

The others are more or less the same.

Upvotes: 1

Related Questions