Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

David Eichel
David Eichel
28,860 Points

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

David Eichel
David Eichel
28,860 Points

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
end

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.