Before we begin. A helpful note is to set
config.use_transactional_fixtures = true in your spec helper. I found that when I wasn’t doing this, Factory Girl was creating duplicate instances of my models and because of that, validations were failing.
So recently at work I’ve been refactoring the user models into polymorphic associations. Part of that process was introducing new factories for these polymorphic associations in Factory Girl. The associations are as follows:
Breaking down the models this way allows us to seperate the concerns of authentication, profile information and teacher/student specific information into their own datasets. I favour this over STI because here, there are no
nil columns anywhere.
So the question remains, how do we create factories for such associations? Well first we need to create a profile and account factory and specify them as polymorphic:
We associate the factories to their polymorphicable (is that a word?), because in the model, they belong to their polymorphicable. Now it’s time to create the student and teacher factories:
For the sake of scrolling, I won’t create the teacher factory because it’s the same idea. When creating the factories for the models that have the polymorphic model, you simply create the polymorphic model in an
after(:create) block and specify the polymorphicable to be the model the factory is for.
TADA. Polymorphic associations in FactoryGirl… Done!