Title: Rails / Rspec - writing spec involving custom validation and belongs_to associations
Tags: ruby-on-rails;factory-bot;rspec-rails
Question: 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 < ActiveRecor181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16Base
  belongs_to :league
  belongs_to :home_team, :class_name =&gt; 'Team', :foreign_key =&gt; :home_team_id
  belongs_to :away_team, :class_name =&gt; 'Team', :foreign_key =&gt; :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 factories:

```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 =&gt; :team
    association :away_team, :factory =&gt; :team
  end
end
```

So with all that, how would I go about writing a spec for the same_league validation method?

```describe Event do
  pending 'should not allow home_team and away_team to be from two different leagues'
end
```

My issue is knowing what the simplest way to go about creating two teams in different leagues and associating one with home_team and the other with away_team in the event model.


Here is the accepted answer: You can store instances you generate with factories and then explicitly use their ID's to fill in the foreign keys for subsequent factories. 

Here I'm creating two leagues, then setting up two tests. One where the event has two teams in the same league and another with two teams in different leagues. This way I can test if the event object is properly validating:

```describe Event do
  before(:each) do
    @first_league = Factory(:league)
    @second_league = Factory(:league)
  end
  it "should allow the home_team and away_team to be from the same league" do
    home_team = Factory(:team, :league_id =&gt; @first_league.id)
    away_team = Factory(:team, :league_id =&gt; @first_league.id)
    event = Factory(:event, :home_team_id =&gt; home_team.id, :away_team_id =&gt; away_team.id)
    event.valid?.should == true
  end
  it "should not allow the home_team and away_team to be from two different leagues" do
    home_team = Factory(:team, :league_id =&gt; @first_league.id)
    away_team = Factory(:team, :league_id =&gt; @second_league.id)
    event = Factory(:event, :home_team_id =&gt; home_team.id, :away_team_id =&gt; away_team.id)
    event.valid?.should == false
  end
end
```



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:02/03/12', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<ruby-on-rails><factory-bot><rspec-rails>', 'Title': 'Rails / Rspec - writing spec involving custom validation and belongs_to associations', 'ViewCount': 1335, 'id': 9123335, 'user_AboutMe': '', 'user_AccountId': 91529.0, 'user_DisplayName': 'keruilin', 'user_Reputation': 16044.0, 'user_Views': 591.0, 'user_id': 251257.0}}