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

Documentation for using a block when creating multiple records misleading #1479

Closed
jackrg opened this issue Mar 5, 2021 · 7 comments
Closed
Labels

Comments

@jackrg
Copy link

jackrg commented Mar 5, 2021

Description

The documentation that covers passing a block to create_list and build_list has an example that only works for build_list.

Reproduction Steps

Look at the section on Creating Multiple Records. The example where there is a block uses attribute assignment. That is fine if you're using build_list, but if you're using create_list that does not work. With create_list, you'll have to use rec.update(...) or something similar, as it appears that the record has already been persisted.

Expected behavior

I expected the example to work for both build_list and create_list

Actual behavior

It only works for build_list.

System configuration

factory_bot version: 5.2.0
rails version: 5.1.7
ruby version: 2.4.10

@jackrg jackrg added the bug label Mar 5, 2021
@entcheva
Copy link

Hi @jackrg - do you mind using our reproduction script to share an executable example where using create_list is failing? I've tried a few examples and have been unable to reproduce the issue you're describing. Thanks!

@jackrg
Copy link
Author

jackrg commented Mar 22, 2021 via email

@composerinteralia
Copy link
Collaborator

I think this is a duplicate of #1444 (comment). There's definitely still room to improve the documentation.

I think you missed the point, if that was your attempt to reproduce it.

I hope you will consider that this sentence comes across to me as fairly rude. I don't think Christina missed the point—the reproduction script shared in that comment is the standard reproduction script shared in our bug report template. It's generally easier for us to figure out what's going on if we have something we can run.

Thank you!

@jackrg
Copy link
Author

jackrg commented Mar 23, 2021 via email

@dan-drew
Copy link

dan-drew commented Jul 8, 2021

IMHO this isn't a documentation issue, it's a bug. The expectation for create_list is that it returns a list of persisted objects, not a list of persisted objects with unpersisted changes. I'd also suggest the most useful reason to pass a block function is to make changes you either want or need to make before each item is persisted. It's easy enough to make changes after the list is returned without needing a hook.

FWIW I've tripped over this multiple times, far enough apart that I forget about this quirk. It's wasted a day or two each time trying to figure out why all the test objects look correct but DB queries are returning the wrong things. If you don't agree with it being a bug then adding a one-line note to the docs would be appreciated.

Cheers
Dan

rgould added a commit to rgould/factory_bot that referenced this issue Dec 8, 2021
Fixes thoughtbot#1444 and thoughtbot#1479

Add a short note in GETTING_STARTED.md about the need to save the record
after it has been modified
composerinteralia pushed a commit that referenced this issue Jan 14, 2022
…1522)

Fixes #1444 and #1479

Add a short note in GETTING_STARTED.md about the need to save the record
after it has been modified
@composerinteralia
Copy link
Collaborator

Closed by fc098ff

I agree there's a case to be made for changing the behavior here, but it may be difficult to do in a backwards compatible way. At least now we have documentation for the current behavior.

@jackrg
Copy link
Author

jackrg commented Jan 14, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants