MySQL中的约会记录

mysql> select * from appointment where id = 2 \G
*************************** 1. row ***************************
                                 id: 2
                       care_unit_id: NULL
           provider_organization_id: 2
                         patient_id: 12
                       clinician_id: 10
                          public_id: NULL
                appointment_type_id: 3
                    scheduled_by_id: 3
              appointment_status_id: 1
appointment_status_change_reason_id: NULL
               scheduled_start_time: 2014-07-23 05:48:00
                      check_in_time: NULL
                     check_out_time: NULL
                 dictation_recorded: NULL
              dictation_transcribed: NULL
                 documentation_time: NULL
                           comments:
                      documentation: NULL
                         created_at: 2014-07-23 02:48:54
                         updated_at: 2014-07-23 05:48:54
                       lock_version: 0
                      reminder_sent: 0
1 row in set (0.00 sec)


Rails中的相同记录

1.9.1 :058 > Appointment.find(2)
  Appointment Load (45.3ms)  SELECT `appointment`.* FROM `appointment` WHERE `appointment`.`id` = 2 LIMIT 1
 => #<Appointment id: 2, care_unit_id: nil, provider_organization_id: 2, patient_id: 12, clinician_id: 10, public_id: nil, appointment_type_id: 3, scheduled_by_id: 3, appointment_status_id: 1, appointment_status_change_reason_id: nil, scheduled_start_time: "2014-07-23 05:48:00", check_in_time: nil, check_out_time: nil, dictation_recorded: nil, dictation_transcribed: nil, documentation_time: nil, comments: "", documentation: nil, created_at: "2014-07-23 02:48:54", updated_at: "2014-07-23 05:48:54", lock_version: 0, reminder_sent: false>
1.9.1 :059 >
1.9.1 :060 >   Appointment.find(2).created_at
  Appointment Load (0.4ms)  SELECT `appointment`.* FROM `appointment` WHERE `appointment`.`id` = 2 LIMIT 1
 => Tue, 22 Jul 2014 22:48:54 EDT -04:00
1.9.1 :061 >


如您所见,created_at相差一天。

现在的问题是我无法按预期使用created_at构成查询

1.9.1 :073 >   Appointment.where("created_at >= ? ",Appointment.find(2).created_at.beginning_of_day)
  Appointment Load (0.6ms)  SELECT `appointment`.* FROM `appointment` WHERE `appointment`.`id` = 2 LIMIT 1
  Appointment Load (0.3ms)  SELECT `appointment`.* FROM `appointment` WHERE (created_at = '2014-07-22 04:00:00' )
 => []
1.9.1 :074 >


如您所见,上面的查询应该返回一条记录,但是created_at值将其收起。

我想知道Rails用户如何解决这种情况。谢谢

最佳答案

数据库中存储的时间始终为UTC。

Rails应用程序读取的时间会有所不同,具体取决于将您的应用程序时区设置为-,并将您的时区设置为UTC -4,因此与数据库存储时相比,Rails应用程序要读取它们的时间要早​​四个小时。这是完全正常的。时间是相同的,只是在不同的时区。

这实际上会给您造成任何问题吗?

关于mysql - Rails和MySQL不同的时区会影响查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24904324/

10-12 16:30