From 8ceea2f885bbc776d7b5c759dbd6e1e69037b0e6 Mon Sep 17 00:00:00 2001 From: Daniel Colson Date: Sun, 26 Apr 2020 21:27:15 -0400 Subject: [PATCH] Document transient associations [ci skip] Before this commit it was not documented that transient associations are not supported. This commit documents that, and provides a workaround. --- GETTING_STARTED.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/GETTING_STARTED.md b/GETTING_STARTED.md index 7db1390..4baa546 100644 --- a/GETTING_STARTED.md +++ b/GETTING_STARTED.md @@ -29,6 +29,7 @@ Getting Started + [With other attributes](#with-other-attributes) + [With `attributes_for`](#with-attributes_for) + [With callbacks](#with-callbacks) + + [With associations](#with-associations) * [Method Name / Reserved Word Attributes](#method-name--reserved-word-attributes) * [Inheritance](#inheritance) + [Nested factories](#nested-factories) @@ -58,7 +59,7 @@ Getting Started + [Attribute precedence](#attribute-precedence) + [In child factories](#in-child-factories) + [Using traits](#using-traits) - + [With associations](#with-associations) + + [With associations](#with-associations-1) + [Traits within traits](#traits-within-traits) + [With transient attributes](#with-transient-attributes) * [Callbacks](#callbacks) @@ -410,6 +411,25 @@ create(:user, upcased: true).name #=> "JOHN DOE" ``` +### With associations + +Transient [associations](#associations) are not supported in factory\_bot. +Associations within the transient block will be treated as regular, +non-transient associations. + +If needed, you can generally work around this by building a factory within a +transient attribute: + +```ruby +factory :post + +factory :user do + transient do + post { build(:post) } + end +end +``` + Method Name / Reserved Word Attributes -------------------------------