Based on RailsCast #323 and #325