Forum: Ruby on Rails Maintain foreign key linkages in fixtures...

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
D8f6a28fbdee08dec747b55d16ffc7eb?d=identicon&s=25 Eric Nielsen (nielsene)
on 2006-06-12 17:17
(Received via mailing list)
Is there anyway to refer to the entities being linked in a foreign key
field of
a fixture by name instead of id?

Ie, if I have
  id: 1
  name: Foo
  id: 2
  name: Bar

  id: 1
  category_id: <%= categories(:some_cat).id %>
  name: Hello World

The above doesn't work due to scope, even if the fixtures are loaded in
proper order.  But I think it illustrates what I'd like to do.  If the
in my fixtures have "good" names, then I'd rather use them when defining
objects that use them -- I can remember the names much better than I can
remember the arbitrary numbers.

I was able to get something of the form:
  id: 1
  category_id: <%= (Category.find_by_name "Foo").id %>
  name: Hello World

to work, but that's adding an extra query to the overhead of
establishing the
fixture.  I can probably take that hit on the small project I'm working
on now,
but on other projects it would probably force me to run smaller suites
than I'd

Is there a better way?

This topic is locked and can not be replied to.