开发者

How to have rails to store time in int format for created_at field automatically

I use rails for version 2.3.5, when i update or save a record, the created_at and updated_at fields can be filled in automatically in the format like '2011-05-17 23:54:53', however i want to store the second int value of time to these fields automatically, what should i do?

Thank you in 开发者_运维问答advance.


Within your database engine the storage method for dates, times, datetimes, and timestamps can vary but the presentation through the SQL layer is often similar. You can transform the request from integer to time if you like using some of the built in functions.

For example, in MySQL you can use the UNIX_TIMESTAMP function to return integer times:

SELECT UNIX_TIMESTAMP(created_at) FROM users

You can also insert integers and convert to times:

UPDATE users SET updated_at=FROM_UNIXTIME(1111885200) WHERE id=1

This sort of thing is not supported natively within ActiveRecord, but you can do conversions using the DateTime class:

User.first.created_at.to_i

You can also convert from an integer to a DateTime:

user.updated_at = Time.at(1111885200)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜