I want to set up some has_many, :through objects using fixtures for testing in rails 3.1.
If you look at the documentation on the association as an example, I want to know how to assign assemblies to parts. The only way I’ve found to do it is to explicitly create the manifest object in it’s own file, but if I’m happy with the defaults on that model and all I want to specify are the part/assembly id’s then is there a way to do it directly?
I want something like:
my_assembly:
parts: my_first_part, my_second_part
This works if you do HABTM, but not when you have the explicit join model.
Never, ever, use Rails fixtures for anything.
I’ll repeat: Never, ever, use Rails fixtures for anything.
They are dangerous, cumbersome and make your tests leak state. They are unsuitable for writing proper tests. At best, you get tests that look as if they are properly written, but have hidden dependencies. The Rails team got this feature 100% wrong, and I wish they’d remove it from Rails so people wouldn’t be tempted to use it.
Instead, install factory_girl_rails and use factories to create your testing records on demand: