Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Getting weird inconsistencies with Time.now and 5.minutes.ago in Ruby/RoR

I have some code and I saved 5.minutes.ago in the rails database and this is what is actually saved into the database 2000-01-01 01:07:23 UTC the thing is sometimes it's working and sometimes it's not. If I print 5.minutes.ago OR Time.now to the browser they are both correct. Here is the code that saves the 5.minutes.ago to the db.

let!(:completed_todo_item) { todo_list.todo_items.create(content: "Eggs", completed_at: 5.minutes.ago) }

This is in a rspec test file and is used to run a test to see if a todo item is marked as completed.

Honestly now that I'm looking at the test in Cmder I see that the time is perfectly in sync with what it should be. However the date is not being saved. Perhaps I've set the column up wrong in the DB? How would I go about fixing that if that's the problem?

1 Answer

The database schema was wrong. In the create method in the migration file "odot/db/migrate/add_completed_at_to_todo_items.rb" I typed this in...

def change
    add_column :todo_items, :completed_at, :datatime

Looks good except it should be :datetime not :datatime just a typo but I figured maybe it would help someone else if they had a similar issue. Check this file and make sure everything is spelled correctly. If you end up migrating this file like I did you'll have to do a rollback.

bin/rake db:rollback
bin/rake db:rollback RAILS_ENV=test

Then make the changes in the file and redo the migration.

bin/rake db:migrate
bin/rake db:migrate RAILS_ENV=test

Hope this helps someone.