2016-07-13 5 views
0

Railsプロジェクトで独自のSQLクエリを作成しようとしています。何を試しても、created_atのフォーマットについて不満を持ち続けます。SQLクエリの問題とcreated_atの正しい書式

current_time = Time.current 
values = attribute_values.map { |attribute| "(#{current_item.id},#{attribute},#{current_time},#{current_time})" }.join(',') 
ActiveRecord::Base.connection.execute("INSERT INTO line_item_attributes (line_item_id, product_attribute_id, created_at, updated_at) VALUES #{values}") 

はエラーを返します:レールログの

PG::SyntaxError - ERROR: syntax error at or near "18" 

SQLクエリは次のようになります。

INSERT INTO line_item_attributes (line_item_id, product_attribute_id, created_at, updated_at) VALUES (76,12398,2016-07-13 18:35:30 +0200,2016-07-13 18:35:30 +0200),(76,12394,2016-07-13 18:35:30 +0200,2016-07-13 18:35:30 +0200) 

は、どのように私は、これは、保存するための適切なフォーマットで入手できますか?

答えて

0

ここでTIMESTAMPを使用してインポートをクリーンアップすることをお勧めします。 RailsにcurrentTime = Time.nowのような変数を与えてからvalues = attribute_values.map { |attribute| "(#{current_item.id},#{attribute},TIMESTAMP '#{currentTime}', TIMESTAMP'#{currentTime}')" }.join(',')