RE: Re: Fixtures and Relationships

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! :slight_smile:

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.

An omap is essentially an ordered hash in yaml if that helps.


Having recently had a solution to this issue presented to me by a
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
data structures. It may not necessarily be the answer to the question
were asking, but it’s useful knowledge nonetheless. details how to set up
YAML file to order the database rows you’ve defined using an Omap. While
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
    id: 2
    parent_id: 1
    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.

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

rake aborted!
a YAML error occured parsing test/fixtures/users.yml. Please note that
be consistently indented using spaces. Tabs are not allowed. Please
have a
at YAML Ain't Markup Language
The exact error was:
YAML::Error: Invalid !omap: {“-nikki”=>{“salt”=>245481,
065, “username”=>“nikki”, “screen_name”=>“nikki”, “email_address”=>"
com"}, “-honemaster”=>{“salt”=>24598, “hashed_password”=>814677,
nemaster”, “screen_name”=>“honemaster”, “email_address”=>"
[email protected]"}
, “-hansgouer”=>{“salt”=>631182, “hashed_password”=>328485,
er”, “screen_name”=>“hansgouer”,
“email_address”=>“[email protected]”},
erman”=>{“salt”=>948687, “hashed_password”=>287631,
reen_name”=>“kilterman”, “email_address”=>“[email protected]”},
alt”=>194883, “hashed_password”=>736319, “username”=>“zenman”,
enman”, “email_address”=>“[email protected]”},
“-rawfingers”=>{“salt”=>342450, “h
ashed_password”=>855412, “username”=>“rawfingers”,
“email_address”=>“[email protected]”}, “-herro”=>{“salt”=>976814,
sword”=>158429, “username”=>“herro”, “screen_name”=>“herro”,
[email protected]”}, “-gymrat”=>{“salt”=>981951, “hashed_password”=>987702,
me”=>“gymrat”, “screen_name”=>“gymrat”,
“email_address”=>“[email protected]”},
roo”=>{“salt”=>878098, “hashed_password”=>843882, “username”=>“roo”,
e”=>“roo”, “email_address”=>“[email protected]”},
“-dresserlan”=>{“salt”=>832618, “h
ashed_password”=>22106, “username”=>“dresserlan”,
“screen_name”=>“dresserlan”, "
email_address"=>“[email protected]”}, “-nickta”=>{“salt”=>256946,
sword”=>29039, “username”=>“nickta”, “screen_name”=>“nickta”,
[email protected]"}, “-jason”=>{“salt”=>541275,
ame”=>“jason”, “screen_name”=>“jason”,
“email_address”=>“[email protected]”},
autygirl”=>{“salt”=>554156, “hashed_password”=>57327,
“screen_name”=>“beautygirl”, “email_address”=>“[email protected]”}}

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

screen_names = [
‘nickta’, ‘herro’, ‘hansgouer’, ‘kilterman’, ‘zenman’,
‘rawfingers’, ‘gymrat’, ‘beautygirl’, ‘nikki’, ‘jason’, ‘roo’,
— !omap
<% screen_names.each do |name| %>
-<%= name %>:
username: <%= name %>
hashed_password: <%= rand(1000000) %>
salt: <%= rand(1000000) %>
screen_name: <%= name %>
email_address: <%= name %>
<% end %>


I really appreciate your help on this one. In fact the problem was
width of spaces lining up with the extra space caused by the space
the “-” before the record definition. I had 2 spaces and I needed 3 to
it work. What a nightmare! Thanks again for all your help.

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


I’m not sure if this is your problem, but it might be. YAML files are
sensitive to whitespace versus tabs, and it may behoove you to keep the
pretty clean of excess spaces by ending your non-content generating erb
calls with a - character, which stops them from producing a line break
they’ve been processed. So for the ones that don’t start with <%= … %>
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’,
‘rawfingers’, ‘gymrat’, ‘beautygirl’, ‘nikki’, ‘jason’, ‘roo’,
— !omap
<% screen_names.each do |name| -%>

  • <%= name %>:
    username: <%= name %>
    hashed_password: <%= rand(1000000) %>
    salt: <%= rand(1000000) %>
    screen_name: <%= name %>
    email_address: <%= name %>
    <% end %->

D. Taylor S.,
Reality Technician