RE: Re: Fixtures and Relationships

if you need a certain order, you might handle that with your object
calls… like has_many :order and such. I would never rely on my items
to be in a specific order in the database. Just my .02

+1 on what Brian said.

Now, is there a way to ensure the foreign keys you populate are valid ?
ie… use ActiveRecord within the YAML to get some relevant data ? For
example, consider this setup:

=========================
create_table “cats”, :force => true do |t|
t.column “hair”, :string
end

create_table “dogs”, :force => true do |t|
t.column "legs, :string
end

create_table “vets”, :force => true do |t|
t.column “cats_id”, :integer
t.column “dogs_id”, :integer
t.column “dr_name”, :string
end

I load my fixtures in the proper order, :cats, :dogs, :vets
The cats and dog tables are now populated with valid data.

Now, within my vets fixture, I want to create a bunch of test vets, but
want
to ensure the foreign
keys are populated with valid cat and dog ids. I tried the following,
but
it complains about
the @cats being nil. I’m assuming AR isn’t available here, or… err,
I’m
not sure:

vets.yml

<%
@cats = Cat.find(:all)
@dogs = Dog.find(:all)
%>
<% 1.upto(40) do |i| %>
vet_<%= i %>:
cat_id: <%= rand(@cats.length + 1) %>
dog_id: <%= rand(@dogs.length + 1) %>
dr_name: blah
<% end %>

No dice. Any insight on how to accomplish this would be greatly
appreciated
!

Dylan

Brian,
The need is simply to correctly set up belongs_to relationships
specified
in other yml file correctly. For example take the following:

— users.yml —
<% screen_names = [ ‘foo’, ‘bar’, ‘foobar’ ] %>
<% screen_names.each do |name| %>

<%= name %>:
username: <%= name %>
hashed_password: 12345
salt: 12345
screen_name: <%= name %>
email_address: <%= name %>@gmail.com
<% end %>

The data is loaded fine but foo is not loaded first as you would expect.
Later I load the fixture posts.yml which is a belongs_to :user.
Unfortunately
the record does not link to foo as you would expect.
I have seen references to ordered yaml (Ordered Mapping Language-Independent Type for YAML™ Version 1.1)
but
don’t understand yet how or if that can be used.

BTW, this is purely to setup the db for testing.

Any ideas?

Zack