Create Dependent Associations in FactoryBot
Imagine the following set of models and relationships:
A user can add a time_entry to a job. The time_entry has a task, and that task has a rate which depends upon the job. So I need to validate that the associated time_entry on a job is associated with a rate that is also associated with that job. Basically, I want to make sure the correct rate is being applied to the job.
Models
I was able to configure this validation in my TimeEntry model using the custom associated_rates_jobs method. However, this made building valid factories really difficult. I needed my time_entry factory to be associated with a rate that shared the same job.
In order to do this, I used Transient Attributes.
Transient attributes will be ignored within attributes_for and won’t be set on the model, even if the attribute exists or you attempt to override it.
I added a transient attribute to create a rate from my rate Factory. Then, I used the values from that attribute to dynamically assign the values for the job and task attributes.