Topic: Rails creating weird dates.

I'm following a tutorial from a book called Beginning Rails 3.  It's a blog program.  It creates an articles table with datetime attributes like published_at, updated_at, created_at.

There is an exercise in the book to use rails console and to create a new Article to be inserted into the articles database.  For the three datetime fields it sets the value which is returned by Date.today.  Now my computer's current time is 7:24pm and the date is 2012-03-25 but this is the Article object it inserts upon article.save (notice the times and dates):

INSERT INTO `articles` (`body`, `created_at`, `excerpt`, `location`, `published_at`, `title`, `updated_at`) VALUES ('SQL stands for Structured Query Language', '2012-03-26 00:15:05', NULL, NULL, '2012-03-25 05:00:00', 'Introduction to SQL', '2012-03-26 00:15:05')

Now I have no idea where it got those times from and why there are even two different times.  All three fields are of type datetime.

When I run Date.today in the console it returns the correct date (Sun, 25 Mar 2012)

Can anyone help?
mike

Re: Rails creating weird dates.

I also noticed that if I issue this command I get the correct date for the datetime field I set.  For the two datetime fields I don't specifically set it creates a date time 5 hours into the future.


Article.create(:title => "RubyConf 2010", :body => "The annual RubyConf will take place in..", :published_at => '2010-05-19')
   (0.3ms)  BEGIN
  SQL (0.5ms)  INSERT INTO `articles` (`body`, `created_at`, `excerpt`, `location`, `published_at`, `title`, `updated_at`) VALUES ('The annual RubyConf will take place in..', '2012-03-26 00:29:43', NULL, NULL, '2010-05-19 00:00:00', 'RubyConf 2010', '2012-03-26 00:29:43')
   (56.1ms)  COMMIT

Re: Rails creating weird dates.

It looks like your application stores DateTime in UTC. Or time zone is set to other zone than yours. Check config/application.rb for this.

Re: Rails creating weird dates.

Dzięki Michale,

Znalazłem to w config/application.rb:

config.encoding = "utf-8"

Zlikwidowałem tą linię i nic się nie poprawiło.

-Michał