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.
Hogan, Brian P. (Guest)
on 2006-02-23 22:39
(Received via mailing list)
Gotcha. That makes total sense. It also makes sense because the yml
probably comes from a hash, and hashes aren't ordered. I think you're in
trouble! :)

May need to take a different approach like the one I suggested, and
maybe make one script that builds your test dataset for you before
dumping it to fixtures.

Interesting though.... never thought about that.
D. Taylor S. (Guest)
on 2006-02-23 22:57
(Received via mailing list)
Having recently had a solution to this issue presented to me by a
coworker,
I'm happy to help out and point how to get fixture rows loaded in a
particular order when running tests. This proves useful especially for
tree
data structures. It may not necessarily be the answer to the question
you
were asking, but it's useful knowledge nonetheless.

http://api.rubyonrails.org/classes/Fixtures.html details how to set up
your
YAML file to order the database rows you've defined using an Omap. While
the
documentation for both omap and the fixtures here leaves something to be
desired, essentially you can setup a YAML file thus:

--- !omap
- parent:
    id: 1
-descendent
   id: 2
   parent_id: 1
-another
  id: 3
  parent_id: 2

This will ensure that the database rows are loaded into the test
database in
exactly this order.

Hope this helps you out.

D. Taylor S.,
Reality Technician.
Ezra Z. (Guest)
on 2006-02-24 01:39
(Received via mailing list)
An omap is essentially an ordered hash in yaml if that helps.

-Ezra
Zack C. (Guest)
on 2006-02-24 02:39
(Received via mailing list)
Taylor, Ezra,
  Thanks for your help.  I feel like I'm really close and must have one
small typo/misunderstanding.  I've tried !omap and this is the error I
receive:

rake aborted!
a YAML error occured parsing test/fixtures/users.yml. Please note that
YAML
must
 be consistently indented using spaces. Tabs are not allowed. Please
have a
look
 at http://www.yaml.org/faq.html
The exact error was:
  YAML::Error: Invalid !omap: {"-nikki"=>{"salt"=>245481,
"hashed_password"=>426
065, "username"=>"nikki", "screen_name"=>"nikki", "email_address"=>"
nikki@gmail.
com"}, "-honemaster"=>{"salt"=>24598, "hashed_password"=>814677,
"username"=>"ho
nemaster", "screen_name"=>"honemaster", "email_address"=>"
removed_email_address@domain.invalid"}
, "-hansgouer"=>{"salt"=>631182, "hashed_password"=>328485,
"username"=>"hansgou
er", "screen_name"=>"hansgouer",
"email_address"=>"removed_email_address@domain.invalid"},
"-kilt
erman"=>{"salt"=>948687, "hashed_password"=>287631,
"username"=>"kilterman",
"sc
reen_name"=>"kilterman", 
"email_address"=>"removed_email_address@domain.invalid"},
"-zenman"=>{"s
alt"=>194883, "hashed_password"=>736319, "username"=>"zenman",
"screen_name"=>"z
enman", "email_address"=>"removed_email_address@domain.invalid"},
"-rawfingers"=>{"salt"=>342450, "h
ashed_password"=>855412, "username"=>"rawfingers",
"screen_name"=>"rawfingers",
"email_address"=>"removed_email_address@domain.invalid"}, 
"-herro"=>{"salt"=>976814,
"hashed_pas
sword"=>158429, "username"=>"herro", "screen_name"=>"herro",
"email_address"=>"h
removed_email_address@domain.invalid"}, "-gymrat"=>{"salt"=>981951, 
"hashed_password"=>987702,
"userna
me"=>"gymrat", "screen_name"=>"gymrat",
"email_address"=>"removed_email_address@domain.invalid"},
"-
roo"=>{"salt"=>878098, "hashed_password"=>843882, "username"=>"roo",
"screen_nam
e"=>"roo", "email_address"=>"removed_email_address@domain.invalid"},
"-dresserlan"=>{"salt"=>832618, "h
ashed_password"=>22106, "username"=>"dresserlan",
"screen_name"=>"dresserlan", "
email_address"=>"removed_email_address@domain.invalid"}, 
"-nickta"=>{"salt"=>256946,
"hashed_pas
sword"=>29039, "username"=>"nickta", "screen_name"=>"nickta",
"email_address"=>"
removed_email_address@domain.invalid"}, "-jason"=>{"salt"=>541275,
"hashed_password"=>141391,
"usern
ame"=>"jason", "screen_name"=>"jason",
"email_address"=>"removed_email_address@domain.invalid"},
"-be
autygirl"=>{"salt"=>554156, "hashed_password"=>57327,
"username"=>"beautygirl",
"screen_name"=>"beautygirl", 
"email_address"=>"removed_email_address@domain.invalid"}}

Here is my fixture... am I missing something???

<%
  screen_names = [
      'nickta', 'herro', 'hansgouer', 'kilterman', 'zenman',
'honemaster',
      'rawfingers', 'gymrat', 'beautygirl', 'nikki', 'jason', 'roo',
'dresserlan'
      ]
%>
--- !omap
<% screen_names.each do |name| %>
-<%= name %>:
  username: <%= name %>
  hashed_password: <%= rand(1000000) %>
  salt: <%= rand(1000000) %>
  screen_name: <%= name %>
  email_address: <%= name %>@gmail.com
<% end %>

Thanks,
Zack
D. Taylor S. (Guest)
on 2006-02-24 03:43
(Received via mailing list)
I'm not sure if this is your problem, but it might be. YAML files are
pretty
sensitive to whitespace versus tabs, and it may behoove you to keep the
file
pretty clean of excess spaces by ending your non-content generating erb
calls with a - character, which stops them from producing a line break
after
they've been processed. So for the ones that don't start with <%= ... %>
try
it like: <% screen_names.each do |name| -%>  (notice the "-" prior to
the %>
)

Also, make sure you are using two spaces rather than a tab character for
your field definitions. Also, make sure there is a space between your
"-"
character prior to the "record name" definition.. When I succesfully got
omap to work today, I increased my "soft tab width" to match towards the
extra characters generated by the "-" in the "named record" area... so
if I
were to do what you're doing, I would have done it like this (though I
haven't tested your particular example):

<%
  screen_names = [
      'nickta', 'herro', 'hansgouer', 'kilterman', 'zenman',
'honemaster',
      'rawfingers', 'gymrat', 'beautygirl', 'nikki', 'jason', 'roo',
'dresserlan'
      ]
-%>
--- !omap
<% screen_names.each do |name| -%>
- <%= name %>:
    username: <%= name %>
    hashed_password: <%= rand(1000000) %>
    salt: <%= rand(1000000) %>
    screen_name: <%= name %>
    email_address: <%= name %>@gmail.com
<% end %->

D. Taylor S.,
Reality Technician
Zack C. (Guest)
on 2006-02-24 18:48
(Received via mailing list)
Taylor,
  I really appreciate your help on this one.  In fact the problem was
the
width of spaces lining up with the extra space caused by the space
between
the "-" before the record definition.  I had 2 spaces and I needed 3 to
make
it work.  What a nightmare!  Thanks again for all your help.

BTW I never could get the -%> terminator that suppresses the newline to
work
in the fixture.  I use them all the time in my views.  Who knows...

Zack
This topic is locked and can not be replied to.