-
Notifications
You must be signed in to change notification settings - Fork 97
Handling nested attributes in Fabricate.attributes_for. #215
Comments
Ideally, as there are many case where generating attributes involves nesting attributes for some records, and committing others directly to the database, there would be callbacks available for |
I agree that this is how it should work. There are some architectural decisions in the library that make this change extremely difficult though. I have it on my feature list for 3.0 though. |
How did you go with this end the end @paulelliott? |
As it stands there is not a solution for this. I closed out all old lingering issues last week but since you're still interested I'll reopen it 😄 This is a challenging issue to address but now that I'm thinking through it again I have some ideas. I'll see what I can do. |
So the issue we have is in reliably knowing when something is an association and when it isn't. Right now it is inferred when you use the Fabricator(:thingy) do
widgets(count: 5) // works
wocket(fabricator: :discombobulator) // works
huzzahs do // doesn't work :(
(1..10).map { Fabricate.build(:huzzah) }
end
end In the case above we could know that Fabricator(:thingy) do
huzzahs(association: true) { ... }
huzzahs(hasMany: true) { ... }
hasMany :huzzahs { ... }
wocket(belongsTo: true) { ... }
belongsTo :wocket { ... }
end I'm kind of leaning towards introducing the Fabricator(:thingy) do
hasMany :huzzahs, count: 2 // builds 2 `huzzah` objects and assigns them
hasMany :huzzahs do // assigns whatever the block returns
(1..10).map { Fabricate.build(:something) }
end
end To be clear, what I am proposing here is a major change and huge chunk of work to take on. I can't promise I would be able to implement it in the near term. |
Hey @paulelliott, is it not possible to determine associations by inspecting the model? Certainly the information is there, can it be used in Fabrication? |
Yeah, it could be. The problem with that is fabrication works with a lot of different systems that all do that differently. To fully support that paradigm would be just as much work as introducing the new construct. |
I like your proposed solution, but could you use |
I have a fabricator defined as so:
The issue I'm facing is when I use
Fabricate.attributes_for(:record)
. Currently the following output results:Whereas I was hoping for output that I could feed into a controller's parameters, and would thus be more like:
The text was updated successfully, but these errors were encountered: