Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update has_many-associations.md #1629

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion docs/src/cookbook/has_many-associations.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,20 @@ create(:user_with_posts).posts.length # 5
create(:user_with_posts, posts_count: 15).posts.length # 15
```

Or, for a solution that works with `build`, `build_stubbed`, and `create`
A simple example that works for build without having to save to a database:
```ruby
trait :with_completed_survey_visit do
after(:build) do |home, evaluator|
survey_visit = build(:survey_visit, home: home) #belongs_to association
# Below line is required for the association to work from home.survey_visits without saving to the database
# Otherwise it only works in one direction: survey_visit.home
home.survey_visits << survey_visit # has_many association
end
end
end
```

Or, for another solution that works with `build`, `build_stubbed`, and `create`
(although it doesn't work well with `attributes_for`), you can use inline
associations:

Expand Down