I have 3 models.
class Team < ActiveRecord::Base
...
has_many :seasons_teams, :dependent => :destroy
has_many :seasons, :through => :seasons_teams
has_and_belongs_to_many :players
...
end
class Season < ActiveRecord::Base
...
has_many :players_seasons, :dependent => :destroy
has_many :players, :through => :players_seasons
has_many :seasons_teams, :dependent => :destroy
has_many :teams, :through => :seasons_teams
...
end
class Player < ActiveRecord::Base
...
has_many :players_seasons, :dependent => :destroy
has_many :seasons, :through => :players_seasons
has_and_belongs_to_many :teams
...
end
There will be validations such that any given player can have at most one team for each season.
I am looking for an efficient way to get a players team for any given season, i.e.:
@player.team(@season)
Thanks!
I think something like this ought to work:
To get the nice concise syntax you’re looking for you would use it in a scope, e.g.: