-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Attribute pairs with names like 'name' and 'name_id' are unsettable. #734
Comments
I would rename the attribute |
Any news on this? |
@mbell697 you're correct, I actually outline this a bit more in depth here as well. Basically, the concept of aliases helps ensure associations aren't incorrectly set given AR's naming conventions. The best solution might be to extract that domain concept into a separate model and manage it as such. |
@mbell697 Since we have two separate entities; Factory Girl and Factory Girl Rails ("factory_girl_rails provides Rails integration for factory_girl")... Shouldn't then Factory Girl aim to be Rails agnostic and hand that part over to factory_girl_rails? Still, FG has gem dependencies on both active_support and active_record, so it is hard to see where vanilla FG ends and the FG:Rails start. Long story short: People expect FG to work with foo and foo_id. For example, lets say that you want to create a factory for a json object/representation of a model with the Hashie::Mash class. We would like an event factory to have both the interaction_id it belongs to, as well as the interaction json object serialized within it:
This may be a json representation of a Rails model, it doesn't matter. It is not unreasonable to have both, and it clearly illustrates the issue. It is today unfortunately not possible to do this with FG. Unless you work around this quirk in one way or another. What would be best of both worlds would not be to opt in or opt out entirely, but to be able to customize it per factory definition (the default being as is) so we could opt out per factory using a keyword e g What do you think? |
This is a bit of an odd one, minimal failing example with a work around available here: https://github.com/mbell697/factory_girl_bug_example (tests are very long winded to allow monkeying around with them independently).
If you have two attributes with names like
thing
andthing_id
they don't appear to be settable at the same time when callingbuild
,create
, etc.For example:
build(:my_model, domain: 'example.com', domain_id: 'ID-32423-234')
Will result in both attributes being
nil
, I've monitored the setters for these attributes, and they are never called (both for plain old ruby classes and mongoid models, tests for both in the linked repo).calling this however:
build(:my_model, domain: 'example.com')
Will set the domain attribute correctly but
domain_id
will remain nil, even if there is a sequence defined for it.If you have sequences setup for both of the attributes, then calling
build(:my_model)
sets them correctly.I couldn't track a specific cause of this in the FG source, my best guess is that there is an assumption somewhere that paired attributes like this represent an association. There are factories with implicit and explicit (add_attribute) attribute definitions in the test repo. The functioning work around is to set these fields to be transient and assign them in an
after(:build)
callback.Edit: This seems to be the result of the default alias definition in: https://github.com/thoughtbot/factory_girl/blob/7e2bed5938be2e1314a7df6a647031a47d01c2c8/lib/factory_girl/aliases.rb
Given that, I assume the transient approach is likely the best work around.
The text was updated successfully, but these errors were encountered: