I have the following AR has_many, belongs_to relationships:
League → Conference → Division → Team
I have an Event model that looks like this:
class Event < ActiveRecord::Base belongs_to :league belongs_to :home_team, :class_name => 'Team', :foreign_key => :home_team_id belongs_to :away_team, :class_name => 'Team', :foreign_key => :away_team_id validate :same_league def same_league return if home_team.blank? || away_team.blank? errors.add :base, "teams must be in the same league" if home_team.league != away_team.league end end
And some plants:
FactoryGirl.define do factory :league do name 'NFL' end end Factory.define :conference do |f| f.name 'NFC' f.association :league end Factory.define :division do |f| f.name 'North' f.association :conference end Factory.define :team do |f| f.name 'Packers' f.locale 'Green Bay' f.association :division end FactoryGirl.define do factory :event do association :league association :home_team, :factory => :team association :away_team, :factory => :team end end
So how to do this, how can I write a specification for the same league check method?
describe Event do pending 'should not allow home_team and away_team to be from two different leagues' end
My problem is to know that the easiest way to create two teams in different leagues and associate them with home_team and the other with away_team in the event model.
source share