# RE: Re: Fixtures and Relationships

**URL:** <https://www.ruby-forum.com/t/re-re-fixtures-and-relationships/72522>\
**Category:** Rails\
**Created:** [February 23, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/re-re-fixtures-and-relationships/72522 "2006-02-23T21:39:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HoganS\_Brian\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de0a6/32.png) [@HoganS\_Brian\_P](https://www.ruby-forum.com/u/HoganS_Brian_P)\
**Post date:** [February 23, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/re-re-fixtures-and-relationships/72522/1 "2006-02-23T21:39:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![HoganS\_Brian\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de0a6/32.png) [@HoganS\_Brian\_P](https://www.ruby-forum.com/u/HoganS_Brian_P)\
**Post date:** [February 24, 2006, 12:39am UTC](https://www.ruby-forum.com/t/re-re-fixtures-and-relationships/72522/2 "2006-02-24T00:39:10Z")

</div>

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

-Ezra

---

<div class="post-metadata">

**Author:** ![HoganS\_Brian\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de0a6/32.png) [@HoganS\_Brian\_P](https://www.ruby-forum.com/u/HoganS_Brian_P)\
**Post date:** [February 23, 2006, 9:57pm UTC](https://www.ruby-forum.com/t/re-re-fixtures-and-relationships/72522/3 "2006-02-23T21:57:50Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![HoganS\_Brian\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de0a6/32.png) [@HoganS\_Brian\_P](https://www.ruby-forum.com/u/HoganS_Brian_P)\
**Post date:** [February 24, 2006, 1:39am UTC](https://www.ruby-forum.com/t/re-re-fixtures-and-relationships/72522/4 "2006-02-24T01:39:46Z")

</div>

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 [YAML Ain't Markup Language](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

---

<div class="post-metadata">

**Author:** ![HoganS\_Brian\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de0a6/32.png) [@HoganS\_Brian\_P](https://www.ruby-forum.com/u/HoganS_Brian_P)\
**Post date:** [February 24, 2006, 5:48pm UTC](https://www.ruby-forum.com/t/re-re-fixtures-and-relationships/72522/5 "2006-02-24T17:48:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![HoganS\_Brian\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de0a6/32.png) [@HoganS\_Brian\_P](https://www.ruby-forum.com/u/HoganS_Brian_P)\
**Post date:** [February 24, 2006, 2:43am UTC](https://www.ruby-forum.com/t/re-re-fixtures-and-relationships/72522/6 "2006-02-24T02:43:09Z")

</div>

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
