It is more than often that you have to write a fixtures for models with polymorphic association. In this short tutorial, I will show you how to use few shortcut trick to create fixtures.
Assume we have following code:
1 2 3 4 5 6 7
Above is just a very simple code declaring a polymorphic association between Company to Car. Next, we will write some fixtures for the sake of testing.
1 2 3 4 5 6 7 8 9 10
We could do better by referencing to Company fixture via named fixture, so the above car fixture would be:
1 2 3
Please be noted that the syntax of line
borrowable now has an additonal
keyword. This is to tell rails that the
borrowable_type is of
Without that, the fixtures will set
nil, thus invalid.
Hope you find this tutorial useful. And please avoid using fixures as much as you can. Use factory_girl or machinist. See you in the next tutorial.