Forum: Ruby on Rails Use fixtures within fixtures?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Heinz S. (Guest)
on 2009-03-28 19:41
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?
Harold (Guest)
on 2009-03-28 20:01
(Received via mailing list)
How about using erb?

<%= find_by_id(<some-id>) %>

On Mar 28, 1:41 pm, Heinz S. <removed_email_address@domain.invalid>
Heinz S. (Guest)
on 2009-03-28 20:11
Harold wrote:
> How about using erb?
>
> <%= find_by_id(<some-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 :) 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.
Phlip (Guest)
on 2009-03-28 20:22
(Received via mailing list)
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...
Heinz S. (Guest)
on 2009-03-28 20:29
<%= Fixture.identify(:one) %> sounds great to me, thank you! :) parent:,
of course... typo.
Heinz S. (Guest)
on 2009-03-28 20:34
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?
Harold (Guest)
on 2009-03-28 20:50
(Received via mailing list)
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>
Frederick C. (Guest)
on 2009-03-28 22:45
(Received via mailing list)
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
Heinz S. (Guest)
on 2009-03-29 17:15
> >> 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
...?
Harold A. Giménez Ch. (Guest)
on 2009-03-29 17:47
(Received via mailing list)
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. <
Heinz S. (Guest)
on 2009-04-01 17:16
Alright, it's working now. Thanks guys!
This topic is locked and can not be replied to.