日期时间值不正确; Ruby on Rails

时间:2014-12-18 01:35:19

标签: ruby-on-rails

我正在使用Ruby on Rails中的邮件程序运行测试,我收到以下错误:

*********-C02MGBVJFD57:myapp ************$ bundle exec rake test:mailers

# Running:

EE

Finished in 0.110500s, 18.0995 runs/s, 0.0000 assertions/s.

  1) Error:
UserMailerTest#test_account_activation:
ActiveRecord::StatementInvalid: Mysql2::Error: Incorrect datetime value: '2014-12-18 01:07:49 UTC' for column 'activated_at' at row 1: INSERT INTO `users` (`name`, `email`, `password_digest`, `admin`, `activated`, `activated_at`, `created_at`, `updated_at`, `id`) VALUES ('Michael Example', 'michael@example.com', '$2a$04$hz6pvZctid6gZEuv0.qAe.0mEfbEjxso9GrXwC3yHRhIThIJ7Vx5m', 1, 1, '2014-12-18 01:07:49 UTC', '2014-12-18 01:07:49', '2014-12-18 01:07:49', 762146111)



  2) Error:
UserMailerTest#test_password_reset:
ActiveRecord::StatementInvalid: Mysql2::Error: Incorrect datetime value: '2014-12-18 01:07:49 UTC' for column 'activated_at' at row 1: INSERT INTO `users` (`name`, `email`, `password_digest`, `admin`, `activated`, `activated_at`, `created_at`, `updated_at`, `id`) VALUES ('Michael Example', 'michael@example.com', '$2a$04$XTAWn5P9kikgOXqxQgiJqOjP027p4HnucHZ6pth2aaLwolaFcehsm', 1, 1, '2014-12-18 01:07:49 UTC', '2014-12-18 01:07:49', '2014-12-18 01:07:49', 762146111)


2 runs, 0 assertions, 0 failures, 2 errors, 0 skips

当我检查我的fixtures users.yml文件时,我看到我正在使用时区ruby戳:

michael:
  name: Michael Example
  email: michael@example.com
  password_digest: <%= User.digest('password') %>
  admin: true
  activated: true
  activated_at: <%= Time.zone.now %>

archer:
  name: Sterling Archer
  email: duchess@example.gov
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>

lana:
  name: Lana Kane
  email: hands@example.gov
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>

mallory:
  name: Mallory Archer
  email: boss@example.gov
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>

<% 30.times do |n| %>
user_<%= n %>:
  name:  <%= "User #{n}" %>
  email: <%= "user-#{n}@example.com" %>
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>
<% end %>  

和我的db / seeds.rb文件如下所示:

User.create!(name:  "Example User",
             email: "example@railstutorial.org",
             password:              "foobar",
             password_confirmation: "foobar",
             admin: true,
             activated: true,
             activated_at: Time.zone.now)

99.times do |n|
  name  = Faker::Name.name
  email = "example-#{n+1}@railstutorial.org"
  password = "password"
  User.create!(name:  name,
               email: email,
               password:              password,
               password_confirmation: password,
               activated: true,
               activated_at: Time.zone.now)
end

编辑:这是测试

测试/邮寄者/ user_mailer_test.rb:

require 'test_helper'

class UserMailerTest < ActionMailer::TestCase

  test "account_activation" do
    mail = UserMailer.account_activation
    assert_equal "Account activation", mail.subject
    assert_equal ["to@example.org"], mail.to
    assert_equal ["from@example.com"], mail.from
    assert_match "Hi", mail.body.encoded
  end

  test "password_reset" do
    mail = UserMailer.password_reset
    assert_equal "Password reset", mail.subject
    assert_equal ["to@example.org"], mail.to
    assert_equal ["from@example.com"], mail.from
    assert_match "Hi", mail.body.encoded
  end
end

我做错了吗?谢谢您的帮助。

6 个答案:

答案 0 :(得分:5)

您的迁移文件中的activate_at是否为datetime?您可以尝试Time.zone.now.to_datetime

答案 1 :(得分:1)

或者您只需在灯具中使用固定的created_at时间:

created_at: "2015-08-15 20:53:32"

答案 2 :(得分:1)

我正在使用MySQL 5.7.10。尝试使用ModelName.connection.execute(SQL语句)将记录直接插入到表中时,我遇到了类似的问题,包括created_at和updated_at时间戳。

这对我有用:

'#{DateTime.now.strftime('%F %T')}'

请务必包含周围的单引号。

答案 3 :(得分:0)

我怀疑您的专栏属于&#39; datetime&#39;。 使用&#39; DateTime.now.in_time_zone&#39;而不是&#39; Time.zone.now&#39;

答案 4 :(得分:0)

我有同样的错误。但我已在 seeds.rb users.yml 中更新datetime部分Timezone.now.to_datetime,然后运行:

$ bundle exec rake db:migrate:reset
$ bundle exec rake db:seed

然后运行

$ bundle exec rake test

你想要得到所有的绿色。

答案 5 :(得分:0)

ActiveRecord :: Base.sanitize将采用ruby Time对象并正确转换为带引号的sql值

For i As Integer = 0 To dgvFields.Rows.Count - 1
       If dgvfields.Rows(i).Cells("THE COLUMN NAME").Value="ValueA" Then
          'do something
       End if    
Next