fixtures in edge and polymorphic associations

90 views
Skip to first unread message

Dan

unread,
Nov 16, 2007, 1:03:19 PM11/16/07
to Ruby on Rails: Talk
Hello,

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
Rathole.

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?

John Barnette

unread,
Nov 16, 2007, 4:40:42 PM11/16/07
to rubyonra...@googlegroups.com
On Nov 16, 2007, at 10:03 AM, Dan wrote:
> 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.

Hi Dan,

You can insert the generated ID for any fixture using ERB:

foo:
bar_id: <%= Fixtures.identify(:the_target_fixture_label) %>

As far as polymorphic assoiations go, take a look at this patch:
http://dev.rubyonrails.org/ticket/10183


~ j.

Tiago Macedo

unread,
Nov 18, 2007, 5:59:18 PM11/18/07
to rubyonra...@googlegroups.com
You actually don't need the patch to support polymorphic relationships.

The patch suggests this:

# instead of saying
foo:
pet_id: 1 # a pet fixture labelled "horace"
pet_type: Ferret

# you can say
foo:
pet: horace (Ferret)


But in fact you can do this:

foo:
pet: horace
pet_type: Ferret

This is good enough for me.


Tiago Macedo

Dan

unread,
Nov 19, 2007, 2:55:00 AM11/19/07
to Ruby on Rails: Talk
Thanks for the replies.

I've been trying this syntax

> foo:
> pet: horace
> pet_type: Ferret

and the relationship isn't resolving. This is in edge and I don't
need a patch?

Tiago Macedo

unread,
Nov 19, 2007, 6:55:09 AM11/19/07
to rubyonra...@googlegroups.com
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:
http://portugueserb.rubyforge.org/svn/plugins/mixed_fixtures/

Tiago Macedo

Reply all
Reply to author
Forward
0 new messages