我使用parseexcel gem来读取excel文件.. 只要我将东西打印到控制台,每件事情都可以正常工作。 我创建了一个rake任务来将数据插入到db ..所以我有一堆创建语句。像这样Ruby/Rails强制转换为字符串错误
description = row[i+1].to_s
Coa.create(:is_group => true, :ag_gl_code => "AGL_001", :description => "#{description}")
我的问题是'description'变量。它是一个字符串字段。如果我描述打印到控制台这样它的工作原理,并打印值
puts "{#description}" #prints the value 'Estate'
但在创建它给了我一个SQL错误
SQLite3::SQLException: unrecognized token: "'E":
,但生成的查询罚款。该字符串在查询中使用单引号,并且如果用硬编码字符串替换该变量,则它工作得很好。我曾尝试过很多组合,例如使用to_s等等,但无法让它起作用。
任何帮助,将不胜感激。在这里感谢
是我的架构
create_table "coas", :force => true do |t|
t.string "ag_gl_code"
t.string "description"
t.boolean "is_group"
t.datetime "created_at"
t.datetime "updated_at"
end
这里是终端的日志,我已经登录的“说明”的值,注意它打印的价值,并没有额外的报价。
Estate
rake aborted!
SQLite3::SQLException: unrecognized token: "'E": INSERT INTO "coas" ("created_at", "is_group" , "description", "ag_gl_code", "updated_at") VALUES ('2011-07-14 14:48:56.616150', 't', 'Estate', 'AGL_001', '2011-07-14 14:48:56.616150')
你为什么要像':description =>“#{description}”''那样插入字符串?只需':description => description'就足够了。而你的错误信息表明有人没有正确引用事情。 –
'puts“{#description}”'错字? – Dogbert
我试过:description =>描述,给出了同样的错误,但是如果我把:description =>'estate',它工作正常。 (投入是我在这里创建的类型) –