I've been using the fixture enhancements in edge rails to manage
associations between objects. It has been working great except for
polymorphic associations. I saw that they weren't supported in
I read some post that the id's generated by the fixtures are constant
so I just pulled the generated id's and manually set the polymorphic
id and the polymorphic type in the fixture as well.
This worked great locally on my machine, but when I moved the
application to a staging are the generated id's were different,
breaking all of the associations.
Anyone have any better luck with another approach?
You can insert the generated ID for any fixture using ERB:
bar_id: <%= Fixtures.identify(:the_target_fixture_label) %>
As far as polymorphic assoiations go, take a look at this patch:
You actually don't need the patch to support polymorphic relationships.
The patch suggests this:
# instead of saying
pet_id: 1 # a pet fixture labelled "horace"
# you can say
pet: horace (Ferret)
But in fact you can do this:
This is good enough for me.
John Barnette wrote:
Thanks for the replies.
I've been trying this syntax
and the relationship isn't resolving. This is in edge and I don't
need a patch?
It's working for me in edge, as long as I don't specify ids for fixtures
(currently this will disable foxy fixtures).
There is a patch in the Rails Trac that allows you to specify ids and
still use foxy fixtures: http://dev.rubyonrails.org/ticket/10004/
As I need this patch and don't want to use a monkey patched version of
Rails, I turned this into a plugin: