0

我已经建立了一个导入数据的任务,如下所示:

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,我尝试过使用不同的 PostgreSQL布尔数据类型但没有工作。我错了什么?

4

3 回答 3

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 有帮助。

于 2012-11-07T11:23:02.207 回答
0

尝试用真/假替换真/假

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 
于 2012-11-07T09:53:24.727 回答
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'
于 2012-11-07T10:00:45.140 回答