title: How to avoid circular creation of associated models in factory_girl?
body: <p>I have an app where a user can sign in with multiple services, e.g. Google Plus, Facebook, Twitter, etc.</p>
  
  <p>To facilitate this, I have a base <code>User</code> model which <code>has_many</code> <code>Identity</code> records. </p>
  
  <ul>
  <li>Each <code>Identity</code> record has a <code>provider</code> field (e.g. <code>"Google"</code>, <code>"Facebook"</code>, etc...) to indicate what provider is used to sign in.</li>
  <li>There's an ActiveRecord validation that only lets a user have one of each type of provider. So a user can't have 2 <code>"Google"</code> <code>identities</code>.</li>
  </ul>
  
  <p>I set up my factories as follows:</p>
  
  <pre><code>FactoryGirl.define do
    factory :user do
      sequence(:name) { |n| "Julio Jones-#{n}"}
      sequence(:email) { |n| "julio.jones-#{n}@atl.com" }
  
      after(:create) do |user|
        create(:identity, user: user)
      end
    end
  
    factory :identity do
      user
  
      provider "Google"
      email { user.email }
      password "password"
    end
  end
  </code></pre>
  
  <p>The <code>User</code> model has a callback that creates an <code>Identity</code> record. It works great when running</p>
  
  <pre><code>user = FactoryGirl.create(:user)
  </code></pre>
  
  <p>However, if I create the <code>identity</code> instead</p>
  
  <pre><code>identity = FactoryGirl.create(:identity)
  </code></pre>
  
  <p>the <code>identity</code> factory will first try to create a parent <code>user</code>, which will in turn create another <code>identity</code>. When it finally gets back to creating the <code>identity</code> I made the call to, another <code>identity</code> already exists with the same <code>provider</code> for that <code>user</code> and it fails.</p>
  
  <p>Essentially, I need a way for the <code>after(:create)</code> callback to NOT trigger when the <code>user</code> is being created by the <code>:identity</code> factory. Is there a way to tell what made the call to create a particular factory?</p>