Forum: Ruby on Rails RE: Re: Fixtures and Relationships

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.
Hogan, Brian P. (Guest)
on 2006-02-23 19:31
(Received via mailing list)
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
Dylan S. (Guest)
on 2006-02-23 20:31
(Received via mailing list)
+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

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

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

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
to ensure the foreign
keys are populated with valid cat and dog ids.  I tried the following,
it complains about
the @cats being nil.  I'm assuming AR isn't available here, or... err,
not sure:

       @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
Zack C. (Guest)
on 2006-02-23 22:08
(Received via mailing list)
  The need is simply to correctly set up belongs_to relationships
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 %>
<% 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.
the record does not link to foo as you would expect.
I have seen references to ordered yaml (
don't understand yet how or if that can be used.

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

Any ideas?

This topic is locked and can not be replied to.