Alias a route in Rails

Once in a while, I want a route but don’t want to change the controller name or any actual filenames. Route alias to the rescue. If I have a controller called ‘children’ but want a route called ‘kids’ so I can have a url like /kids/1234 then I would add this to my routes file:

resources :children, path: 'kids'

The only caveat is that your route helpers should still refer to the resource, so children_path(), not kids_path().

Ruby Time Bug

At work today, I was stuck on an odd bug that had to deal with a datetime column. Or so I thought. Perhaps I just discovered the secret to time travel.


>> "2010-10-10".to_time
=> Sun Oct 10 00:00:00 UTC 2010
>> "112-01-20".to_time
=> Fri Jan 20 00:00:00 UTC 2012

Apparently, there’s some weirdness with Ruby and the Time class where if you want the years 0-138AD, you can’t have them. That’s because they don’t exist. What you actually get when you try to turn the year 112AD into a Time object is space travel to the year 2012.

How to get around that? Use DateTime.

>> "112-01-20".to_datetime
=> Tue, 20 Jan 0112 00:00:00 +0000