我已经构建了一个导入数据的任务:
CSV.foreach(file, :headers => true) do |row|
question = Question.new ({
content: row[0],
mark: row[1],
topic_id: row[2],
question_type_id: row[3],
answers_attributes: [
{ content: row[5], correct: row[6] },
{ content: row[7], correct: row[8] }
]
})
question.user_id = row[4]
question.save!
这是我的CSV文件:
content, mark, topic_id, question_type_id, user_id, content, correct, content, correct
_____ are people involved in or affected by project activities, 5, 16, 1, 3, True, 't', False, 'f'
但是当我运行导入任务时,correct attribute
在数据库中始终为FALSE,我尝试使用不同的Boolean Data Type PostgreSQL但不能正常工作。我错了什么?
答案 0 :(得分:1)
我发现了问题。这是csv文件有问题:
content, mark, topic_id, question_type_id, user_id, content, correct, content, correct
_____ are people involved in or affected by project activities, 5, 16, 1, 3, True, 1, False, 0
这里的问题是布尔列值之前的空格。在上面的csv文件中,True, 1
之间的空格会产生问题。当csv文件导入到数据库时,它包含1
前面的空格,因此值已导入:" 1"
,而不是"1"
,因此postgresql无法识别带空格的值。我删除了它之前的空间,它现在工作,这是我正确的csv文件(我删除了所有空格):
content,mark,topic_id,question_type_id,user_id,content,correct,content,correct
_____ are people involved in or affected by project activities,5,16,1,3,True,1,False,0
如果在CSV文件中有像空格一样的问题,也许对SO有帮助。
答案 1 :(得分:0)
尝试用true / false替换True / False
1.9.3-p194 :006 > True
NameError: uninitialized constant True
from (irb):6
from /Users/SAdvincula/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'
1.9.3-p194 :007 > true
=> true
答案 2 :(得分:0)
更改您的csv文件
content, mark, topic_id, question_type_id, user_id, content, correct, content, correct
_____ are people involved in or affected by project activities, 5, 16, 1, 3, True, '1', False, '0'