Rails fixtures gotcha

Stikis is built using the ruby-on-rails framework. The thing I love about this framework is when you think - “hmm, I wonder how you do that…I’ll give this a go” and it just works. Of course it doesn’t always happen like this - here’s an example for posterity.

I was writing a test that depended on the created_at and updated_at attributes of a record. In my fixture I wrote something like:

1
2
3
4
first:
id: 1
created_at: <%= 10.minutes.ago %>
updated_at: <%= 5.minutes.ago %>

When I ran the tests they failed and it seemed like the reason was that the created_at and updated_at fields were nil. I searched the interweb and found that the problem was I needed to do a bit more work to format the times for the database. Like so:

1
2
3
4
first:
id: 1
created_at: <%= 10.minutes.ago.to_s :db %>
updated_at: <%= 5.minutes.ago.to_s :db %>

The tests still don’t pass - but now it’s because of bugs in my code rather than my fixtures. A much better place to start from :).