Posted about 6 years ago. Visible to the public. Repeats.

FactoryBot: Passing attributes to associated records using transient attributes

FactoryBot.define do factory :parent do transient do child_name nil child_allowed_to_drive false end child do association(: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 = FactoryBot.create(:parent, child_name: 'Lisa').child # => 'Lisa' daughter.allowed_to_drive? # => false son = FactoryBot.create(:parent, child_name: 'Benedikt', child_allowed_to_drive: true).child # => 'Benedikt' son.allowed_to_drive? # => true

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Thomas Klemm
Last edit:
about 1 year ago
by Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Thomas Klemm to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more