I save DateTime.now into datetime column of MySQL DB via Rails', but incorrect value seems saved. The saved value is several tens of seconds past.
My Code:
Rails.logger.info("Start: Calendars.where('user_id = ? and calendar_id = ?', @user.id, calendar_id)")
target = Calendars.where('user_id = ? and calendar_id = ?', @user.id, calendar_id)
Rails.logger.info("End: Calendars.where('user_id = ? and calendar_id = ?', @user.id, calendar_id)")
Rails.logger.info("last_clone_at update start")
now = DateTime.now
target.update_all(last_clone_at: now)
Rails.logger.info(now.strftime("%Y-%m-%dT%H:%M:%S")) # Output by DateTime.now
Rails.logger.info(target.first.last_clone_at) # Saved datetime value
Log output:
2014-06-09 14:39:03 INFO last_clone_at update start
2014-06-09 14:39:03 INFO 2014-06-09T14:39:03 # Output by DateTime.now
2014-06-09 14:39:03 INFO 2014-06-09 14:38:00 +0900 # Saved datetime value
My Calendars model is just as follows:
class Calendars < ActiveRecord::Base
# Validations
validates :user_id, presence: true
validates :calendar_id, presence: true, uniqueness: { scope: :user_id }
end
I use:
Update: Other examples of the log output:
2014-06-09 16:28:54 INFO last_clone_at update start
2014-06-09 16:28:54 INFO 2014-06-09T16:28:54
2014-06-09 16:28:54 INFO 2014-06-09 16:28:01 +0900
2014-06-09 16:28:55 INFO last_clone_at update start
2014-06-09 16:28:55 INFO 2014-06-09T16:28:55
2014-06-09 16:28:55 INFO 2014-06-09 16:28:02 +0900
2014-06-09 16:28:57 INFO last_clone_at update start
2014-06-09 16:28:57 INFO 2014-06-09T16:28:57
2014-06-09 16:28:57 INFO 2014-06-09 16:28:04 +0900
Update2: Time to execute Calendars.where(...)
2014-06-09 17:22:06 INFO Start: Calendars.where('user_id = ? and calendar_id = ?', @user.id, calendar_id) 2014-06-09 17:22:06 INFO End: Calendars.where('user_id = ? and calendar_id = ?', @user.id, calendar_id)
I think it is possible. update_all
might be "heavy" operation for your DB and it takes some time for updating records (check your logs).
Try to change output:
Rails.logger.info(DateTime.now.strftime("%Y-%m-%dT%H:%M:%S"))
target.update_all(last_clone_at: DateTime.now)
Rails.logger.info(target.first.last_clone_at)`
I think in this way you get much "closer" values.
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.