timestamps in rails - datetime or UNIX timestamp?
I was currently presenting an project to my professor in university and he didn't like that my rails app used DATETIME to store its timestamps in the database (he doesn't know or seem to like rails anyway :). He said this is going to lead to problems due to d开发者_高级运维ifferent timezones and I should save it as UNIX timestamps (integers) to the database.
Now I suppose the people who designed rails had a reason to store it like this, and as the timestamps fields are added automatically to any migration, I wanted to ask you guys
a) if I can modify the rails default migrations to use the UNIX timestamp by default
b) about pros and cons of this approach
My opinion is contrary to your professor's. It is easy to properly handle Datetime types and timezone issues with Rails.
a) I'm not sure about changing the default, but here's a related question. Ruby on Rails: why ActiveRecord uses "datetime" type instead of "timestamp" for timestamp fields? b) Here's a related question with a good answer. In Ruby on Rails, what's the difference between DateTime, Timestamp, Time and Date?
Rails 2.2 and later supports i18n (Internationalization) out of box. This includes handling time zones and date time formatting in the database as well as views. Have a look at the rails guide for the internationalization API here.
Specifically, you'll probably be looking into Time helpers. I'd recommend making sure that you're storing UTC format DATETIMES in the db and then using the Time helpers to translate it to the correct time for the locale of your end user.
Specify the timezone in /config/application.rb and if you use the right Time
helpers, Rails will just take care of everything else for you.
精彩评论