thoughtbot/factory_girl — Issue #302: Passing id of first association to second association
Author: JeanMertz
Created: 2012-02-21 21:34:32+00:00
State: open

I am trying to create a record using the following Factory:

```ruby
FactoryGirl.define do
sequence :email do |n|
"mail@company#{n}.com"
end

factory :user do
ignore do
account {}
project {}
role {}
end

email
password "password123"
password_confirmation "password123"

trait :owner do
ignore { role 'Owner' }
end

trait :team_member do
ignore { role 'TeamMember' }
end

trait :collaborator do
ignore { role 'Collaborator' }
end

trait :client do
ignore { role 'Client' }
end

after_create do |user, p|
properties = { user: user, account: p.account,
project: p.project, role: p.role }.delete_if{ |k,v| v.nil? }

FactoryGirl.create(:membership, properties)
end
end

factory :membership do
account
user
role ''
project
end
end
```

This works great, except for the fact that `Project` has a `belong_to` relationship to `Account`. The problem is that I don't know if it's possible/can't find a way to pass the id of the generated Account to the generated Project.

So for example, the `:membership` part would look something like this:

```
factory :membership do
account
user
role ''
association :project, account_id: account.id
end
```

Except, this causes a `#>` error.