Thursday, March 15, 2007

Tips for working with Test Fixtures in Rails

I have a requirement in my migration script that created_at column can not be null.

t.column :created_at, :datetime, :null => false

This cause the Test Fixtures loading to fail. My first attempt to remedy this problem was like this.

created_at: <%= DateTime.now %>

This did not work since the date time format is incorrect. The correct format is year-month-day hour:minute:second.

So, I changed it to

created_at: <%= DateTime.now.strftime('%y-%m-%d %H:%M:%S') %>

and it works.

I don't know if there is a better way to do this but this works for me. Since I went around looking for the solution, I decided to write it down.

1 comment:

Unknown said...

Should be able to do:

Time.now.to_s(:db)