Skip to content

Instantly share code, notes, and snippets.

@thomasklemm
Last active August 29, 2015 14:20
Show Gist options
  • Save thomasklemm/9c56c2eb77118cdf9ea4 to your computer and use it in GitHub Desktop.
Save thomasklemm/9c56c2eb77118cdf9ea4 to your computer and use it in GitHub Desktop.
FactoryGirl: Passing attributes to associated records using transient attributes
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