2015-09-18 32 views
0

我擁有數組類型Time。當我嘗試以下面的方式插入數據IAM得到錯誤。無法將Time對象保存到列時間數組中,在Ruby on Rails中

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 using mysql database。並且還將模式中的這些列序列化。仍然iam得到錯誤

這是代碼亞姆在模型中。

class PunchInOut < ActiveRecord::Base 
    serialize :check_in, Array 
    serialize :check_out, Array 
end 

請幫幫我。

回答

2

你不能在MySQL中存儲一個TIME列中的序列化數組,它需要是VARCHAR或TEXT。

當您序列化ActiveRecord中的字段時,它將轉換爲YAML文本,因此它與非文本列類型不兼容。

您應該僅單次存儲時間和時間戳列等

0

serialize方法需要stringtext類型列

add_column :push_in_outs, :check_in, :string 
add_column :push_in_outs, :check_out, :string 

和Rails連載回這些字符串Time格式

相關問題