Last active
August 29, 2015 14:20
-
-
Save thomasklemm/9c56c2eb77118cdf9ea4 to your computer and use it in GitHub Desktop.
FactoryGirl: Passing attributes to associated records using transient attributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FactoryGirl.define do | |
factory :parent do | |
transient do | |
child_name nil | |
child_allowed_to_drive false | |
end | |
child do | |
create(:child, name: child_name, allowed_to_drive: child_allowed_to_drive) | |
end | |
end | |
factory :child do | |
name 'Child' | |
allowed_to_drive false | |
end | |
end | |
# Usage | |
daughter = FactoryGirl.create(:parent, child_name: 'Lisa').child | |
daughter.name # => 'Lisa' | |
daughter.allowed_to_drive? # => false | |
son = FactoryGirl.create(:parent, child_name: 'Benedikt', child_allowed_to_drive: true).child | |
son.name # => 'Benedikt' | |
son.allowed_to_drive? # => false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment