简体   繁体   中英

What is the best practice for comparing against the current time in Rails?

In the app I'm working on we compare against Time.now in some places, and Database.time in others. I would like to use only one of these throughout the whole application. Unfortunately I haven't been able to find much information on them through google searches. Which is preferred? How are they different from each other? Thanks!

There are a couple of articles on thoughtbot's blog that cover this:

Hopefully those help.

The function Time.now gives you the time in the application server running your Rails app request. This is reliable in most cases.

Rails uses the app server time when filling the updated_at and created_at columns of its models.

I'm not sure about the Database.time function you are referring to... This is probably a class defined in your code or inherited from gem in your app. Because of that I would suggest moving to the Ruby core time functions. In your case: Time.now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM