我有数据类型 Time
的数组。当我尝试按以下方式插入数据时,我收到错误。
2.2.2 :001 > p = PunchInOut.new
=> #<PunchInOut id: nil, employee_id: nil, check_in: [], check_out: [], date: nil, created_at: nil, updated_at: nil, shift_id: nil, shift_name: nil>
2.2.2 :002 > p.check_in << Time.now
=> [2015-09-18 19:25:11 +0530]
2.2.2 :003 > p.save
(0.3ms) BEGIN
SQL (1.2ms) INSERT INTO `punch_in_outs` (`check_in`, `check_out`, `created_at`, `updated_at`) VALUES ('---\n- 2015-09-18 19:25:11.695612520 +05:30\n', '--- []\n', '2015-09-18 13:55:19', '2015-09-18 13:55:19')
Mysql2::Error: Incorrect time value: '---
- 2015-09-18 19:25:11.695612520 +05:30
' for column 'check_in' at row 1: INSERT INTO `punch_in_outs` (`check_in`, `check_out`, `created_at`, `updated_at`) VALUES ('---\n- 2015-09-18 19:25:11.695612520 +05:30\n', '--- []\n', '2015-09-18 13:55:19', '2015-09-18 13:55:19')
(0.1ms) ROLLBACK
ActiveRecord::StatementInvalid: Mysql2::Error: Incorrect time value: '---
- 2015-09-18 19:25:11.695612520 +05:30
' for column 'check_in' at row 1: INSERT INTO `punch_in_outs` (`check_in`, `check_out`, `created_at`, `updated_at`) VALUES ('---\n- 2015-09-18 19:25:11.695612520 +05:30\n', '--- []\n', '2015-09-18 13:55:19', '2015-09-18 13:55:19')
iam使用mysql数据库。并且还在模式中序列化了这些列。我仍然收到错误
这是我在模型中的代码。
class PunchInOut < ActiveRecord::Base
serialize :check_in, Array
serialize :check_out, Array
end
请帮助我。
最佳答案
您不能在 MySQL 中将序列化数组存储在 TIME 列中,它必须是 VARCHAR 或 TEXT。
当您序列化 ActiveRecord 中的字段时,它会转换为 YAML 文本,因此它与非文本列类型不兼容。
您应该在时间和时间戳列等中仅存储单个时间。
关于mysql - 无法将 Time 对象保存到 Ruby on Rails 中的 Time 列数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32654022/