无法Time对象保存到列时间的阵列,在Ruby on Rails的 [英] unable to save Time object into the array of column Time, in Ruby on Rails
问题描述
我有数据类型的数组时间
。当我尝试插入数据以下列方式IAM得到错误。
i having the array of datatype Time
. when i try to insert the data in the following way iam getting the error.
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')
使用mysql数据库IAM。也连载中也情态这些列。依然的IAM收到错误
iam using mysql database. and also serialised these columns in the modal also. still iam getting the error
这是在模型中具有code IAM。
this is the code iam having in the model.
class PunchInOut < ActiveRecord::Base
serialize :check_in, Array
serialize :check_out, Array
end
请帮我。
推荐答案
您不能在MySQL的存储在TIME列序列化阵列,它需要是VARCHAR或TEXT。
You can't store in MySQL a serialized array in a TIME column, it needs be VARCHAR or TEXT.
当你序列化领域的ActiveRecord它转换为文本YAML,所以它不是一个非文本列类型兼容。
When you serialize a field in ActiveRecord it is converted to a YAML text, so it's not compatible with a non-text column type.
您应该只单次存储在时间和时间戳列等等。
You should store only single times in Time and Timestamp columns etc.
这篇关于无法Time对象保存到列时间的阵列,在Ruby on Rails的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!