The documentation that covers passing a block to create_list and build_list has an example that only works for build_list.
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.
I expected the example to work for both build_list and create_list
It only works for build_list.
factory_bot version: 5.2.0
rails version: 5.1.7
ruby version: 2.4.10
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!
Hi Christina,
Attached is the script modified to reproduce the issue. I think you missed the point, if that was your attempt to reproduce it. The key point is the use of a block with the call.
To make it work, I added another table (“replies”) that has a one-to-many relationship with posts. That way, it makes sense to create a list of “replies” to a “post”. The key is that I allow the factory to use the default value for body but then attempt to override it within the block. While that would work with #build_list, it does not work with #create_list, because the block is called after the record is persisted, and therefore the changes are not saved.
I could not actually get this to run on my system, as it was giving me dependency issues with the gem file you provided, but I’m sure it will build and run on your system just fine, if the original file builds and runs.
Your documentation gave this example:
twenty_somethings = build_list(:user, 10) do |user, i|
user.date_of_birth = (20 + i).years.ago
end
And that example works fine, as long as you eventually save the records. But it is implied that you could just change #build_list to #create_list and it would work, but it doesn’t.
Please let me know if you have any further questions.
Jack
On Mar 22, 2021, at 3:15 PM, Christina Entcheva @.*> wrote:
Hi @jackrg https://github.com/jackrg - do you mind using our reproduction script https://github.com/thoughtbot/factory_bot/blob/master/.github/REPRODUCTION_SCRIPT.rb 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!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub https://github.com/thoughtbot/factory_bot/issues/1479#issuecomment-804431592, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHK5AJEOVF7BDYKJZGUQFTTE66RRANCNFSM4YWAOE7A.
I think this is a duplicate of https://github.com/thoughtbot/factory_bot/issues/1444#issuecomment-712722680. 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!
I apologize if that statement came across as rude; that was certainly not my intent. I just wasn’t sure whether the template provided was a standardized template, an attempt to reproduce the problem, or something somewhere in between. Based on your statement, Daniel, I obviously misinterpreted the significance of the script. For a standardized template, it was quite close to what I was referring to.
I understand your point about having a reproducible example; I didn’t include one in the first place because I thought that the point was clear; obviously I was mistaken about that.
Given that the documentation at that point is talking about both build_list and create_list (see the section title), if the one example does not work for both calls, it would probably be advisable to give a second example for #create_list. Where I went wrong is that in seeing that example, I assumed that the block would be called prior to persisting the record (similar to traits and such within q Factory definition).
Anyway, I would characterize this as more of a desirable tweak to the documentation than a bull-blown documentation error.
Thanks for your time.
Jack
On Mar 22, 2021, at 5:19 PM, Daniel Colson @.*> wrote:
I think this is a duplicate of #1444 (comment) https://github.com/thoughtbot/factory_bot/issues/1444#issuecomment-712722680. 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!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub https://github.com/thoughtbot/factory_bot/issues/1479#issuecomment-804488339, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHK5AKMDMU72VGPT2GNYDTTE7M7TANCNFSM4YWAOE7A.
Most helpful comment
I think this is a duplicate of https://github.com/thoughtbot/factory_bot/issues/1444#issuecomment-712722680. There's definitely still room to improve the documentation.
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!