LRBlog

Logical Reality Design: Web Design and Software Development

Rails fixture strings that are all numbers

June 22, 2009

I ran into this one today: If you need to specify a string in a YAML file (fixtures or the like) but that string is all digits, put it in quotes.

The problem YAML file looked like this:

spec/fixtures/people.yml (broken)


one:
funky_database_id: 0000012345

two:
funky_database_id: 0000012346

The trouble with this is that yaml interprets those values as integers, not strings, and Person#funky_database_id is a string column. So Ruby conveniently loads the value as an integer and runs to_s on it before inserting. Worse, because these start with 0, they get translated from octal. So people(:one).funky_database_id comes out "5349". Definitely not what I wanted.

This works as expected:

spec/fixtures/people.yml (fixed)


one:
funky_database_id: "0000012345"

two:
funky_database_id: "0000012346"

Add A Comment