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

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.
60e38de043848f82392062088f191213?d=identicon&s=25 Hogan, Brian P. (Guest)
on 2006-02-23 18: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
Ff82af3238a57fbd1212832ec1a19f28?d=identicon&s=25 Dylan Stamat (Guest)
on 2006-02-23 19: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
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
C4dc94c893471878a105761a9207f29b?d=identicon&s=25 Zack Chandler (Guest)
on 2006-02-23 21:08
(Received via mailing list)
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 (http://yaml.org/type/omap.html)
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
This topic is locked and can not be replied to.