Use fixtures within fixtures?


#1

Heya,

I’m using globalize2 and have following problem with my fixtures:

categories.yml:
one:
parent_id: two
color: #ff00aa

two:
color: #00ff11

three:
parent_id: two
color: #ab00ab

category_translations.yml
one-en:
id: one
locale: en
name: Cars

one-es:
id: one
locale: es
name: Coches

two-en:
id: two
locale: en
name: Start

two-es:
id: two
locale: es
name: Inicio

three-en:
id: three
locale: en
name: Bicycles

three-es:
id: three
locale: es
name: Bicicletas

My problem now is that in my translations table all id’s are NULL which
is kinda obvious cause the fixture has no idea where one, two and three
is. Does anyone know how to load a fixture inside a fixture in order to
use them?


#2

How about using erb?

<%= find_by_id() %>

On Mar 28, 1:41 pm, Heinz S. removed_email_address@domain.invalid


#3

Harold wrote:

How about using erb?

<%= find_by_id() %>

On Mar 28, 1:41�pm, Heinz S. removed_email_address@domain.invalid

First of all: I don’t know the id because it’s set randomly :slight_smile: I could
set it in the other yml and then just use them but I’d actually prefer
using rails generated ids. I could use find_by_whatever but I’d like to
use fixtures only if possible.


#4

Heinz S. wrote:

categories.yml:
one:
parent_id: two
color: #ff00aa

one’s raw ID is a hash of its identifier. Get it with

<%= Fixture.identify(:one) %>

two:
color: #00ff11

three:
parent_id: two

That should be parent: two

Only use Fixture.identify() if you can’t link up the real identifier,
through
either belongs_to or has_and_belongs_to_many. The point is to allow the
Fixture
system to install one database table by reading the minimum possible of
its
associated model, and by reading no other model or fixture file.

If globalize2 can’t track these associations, you must use either
Fixture.identify(), or you must fall back to old-fashioned Rails 1
fixtures,
where everything has an id: with a hand-coded number in it…


#5

<%= Fixture.identify(:one) %> sounds great to me, thank you! :slight_smile: parent:,
of course… typo.


#6

Um well… I use two different yml-files so it doesn’t find the
identifiers from the other files. How do I tell him to look in the other
one?


#7

I don’t get that…it doesn’t really look at any other yml, it just
calculates a hash:

Fixtures.identify(:one)
=> 953125641

Fixtures.identify(:two)
=> 996332877

How are you calling this?

-H

On Mar 28, 2:34 pm, Heinz S. removed_email_address@domain.invalid


#8

On Mar 28, 6:34 pm, Heinz S. removed_email_address@domain.invalid
wrote:

Um well… I use two different yml-files so it doesn’t find the
identifiers from the other files. How do I tell him to look in the other
one?

It doesn’t need to - the value of Fixture.identify(foo) depends only
on foo itself (that’s the whole point: being able to predict what id a
fixture with a given label will have).

Fred


#9

The finder call to the DB is redundant. How about you save yourself the
trip
to the db and just do

one-en:
id: <%= Fixtures.identify(:one) -%>
locale: en
name: cars

On Sun, Mar 29, 2009 at 9:15 AM, Heinz S. <


#10

Fixtures.identify(:one)
=> 953125641

Fixtures.identify(:two)
=> 996332877

Frederick C. wrote:

On Mar 28, 6:34�pm, Heinz S. removed_email_address@domain.invalid
wrote:

Um well… I use two different yml-files so it doesn’t find the
identifiers from the other files. How do I tell him to look in the other
one?

It doesn’t need to - the value of Fixture.identify(foo) depends only
on foo itself (that’s the whole point: being able to predict what id a
fixture with a given label will have).

Fred

Oh, I thought I could call that fixture from the other file directly so
I guess the only thing I can do is

category_translations.yml
one-en:
id: <%= Category.find_by_id(Fixtures.identify(:one)) %>
locale: en
name: Cars

one-es:
id: <%= Category.find_by_id(Fixtures.identify(:one)) %>
locale: es
name: Coches
…?


#11

Alright, it’s working now. Thanks guys!