Use foreign-key like attributes with FactoryBot
An odd issue occurs when trying to use attributes similar to methods you'd expect with Rails ActiveRecord
relationships in FactoryBot
.
Take a relationship named provider
. You would expect the ActiveRecord
object to have methods for provider
and provider_id
.
However, some interesting behavior happens when you legitimately have a reason to have methods with those names, not during an ActiveRecord
relationship.
One will get cleared out when providing the attributes for provider
and provider_id
.
There is a long-running issue regarding this, but for now, this is how I could remedy the situation. Set the methods as transient attributes and merge the attributes during initialization.
Tweet