Rails rails 中用 seeds.rb 导入 json 文件

brucebot · 2013年12月27日 · 最后由 brucebot 回复于 2013年12月28日 · 3489 次阅读

Hi,有一个问题请教:

有这么一个 json 文件,里面的格式是这样的

[[{"linkID": ["/watch?v=hjkVdFa3DH8"], "title": ["Chinese electronics industry now uses unique Danish robots"]},
{"linkID": ["/watch?v=RDvgNB2OZxI"], "title": ["RI Seminar: Rodney Brooks : A New Class of Industrial Robot"]},
{"linkID": ["/watch?v=MeFo-yloxdU"], "title": ["Motoman robots in electronic assembly applications"]},
{"linkID": ["/watch?v=-UkVsJFkdZI"], "title": ["Speed test with industrial robot Motoman UP80"]}]

我想把这些数据导入到我的 railsapp 里面去,我想通过 seeds.rb 导入:

records = Video.new(JSON.parse(File.read('youtube.json')))
records.each do |record|
  Video.create!(record['linkID'],record['title'])
end

执行 rake db:seed 我查了测试用的 sqlite3 数据库,没有一点反应,请教是哪里有问题了?

你的代码写的 Video.new 是什么意思 JSON.parse 完了就是数组了 为什么前面还加个 Video.new

#1 楼 @zj0713001 你的意思是说 `records =JSON.parse(File.read('youtube.json')) 这样就可以了?

#2 楼 @brucebot console 下面试试就知道了 很明显 Video.new 完了是一个 model 对象 响应不到你的 each 方法

#2 楼 @brucebot JSON.parse(xxx) 的结果是一个 Hash 直接作为参数传给 create 方法就可以了

#3 楼 @zj0713001 #4 楼 @allenfantasy 我在 console 下面试了,读取是没有问题的,但是写入的时候总是不成功,我的 video table 里面只有 t.string :linkID t.string :title t.timestamps 这三个,但是我执行的时候报错: ArgumentError: wrong number of arguments (3 for 2)

#5 楼 @brucebot sorry 刚没认真看……你贴的 json 文件是不是一开始多了个中括号?

我去掉了中括号后 JSON.parse 的结果:

{"linkID"=>["/watch?v=hjkVdFa3DH8"], "title"=>["Chinese electronics industry now uses unique Danish robots"]}
{"linkID"=>["/watch?v=RDvgNB2OZxI"], "title"=>["RI Seminar: Rodney Brooks : A New Class of Industrial Robot"]}
{"linkID"=>["/watch?v=MeFo-yloxdU"], "title"=>["Motoman robots in electronic assembly applications"]}
{"linkID"=>["/watch?v=-UkVsJFkdZI"], "title"=>["Speed test with industrial robot Motoman UP80"]}

这个 Hash 的值是个数组,不知道是不是这里的问题。

#6 楼 @allenfantasy 对,我试过删除中括号,执行没有任何报错了,但是数据库中没有任何写入

#7 楼 @brucebot 那上 rails console 试试吧。如果不行就用 binding.pry……

Video.create!(record['linkID'][0],record['title'][0])

#7 楼 @brucebot 问题是因为你的 linkID 对应的不是字符串 是数组

  records = JSON.parse '[{"linkID": ["/watch?v=hjkVdFa3DH8"], "title": ["Chinese electronics industry now uses unique Danish robots"]},{"linkID": ["/watch?v=RDvgNB2OZxI"], "title": ["RI Seminar: Rodney Brooks : A New Class of Industrial Robot"]},{"linkID": ["/watch?v=MeFo-yloxdU"], "title": ["Motoman robots in electronic assembly applications"]},{"linkID": ["/watch?v=-UkVsJFkdZI"], "title": ["Speed test with industrial robot Motoman UP80"]}]'
  records.length   # => 4
  records.first       # => {"linkID"=>["/watch?v=hjkVdFa3DH8"],
"title"=>["Chinese electronics industry now uses unique Danish robots"]}
  records.first["linkID"]    # =>  ["/watch?v=hjkVdFa3DH8"]

  你的代码应该是这样

  records.each do |record|
    Video.create!(linkID: record['linkID'].first, title: record['title'].first)
  end

#10 楼 @zj0713001 #9 楼 @WolfLee 多谢! 可惜只是导入了第一个字母

我在 console 下的测试

1.9.3-p125 :026 > records.length
 => 562
1.9.3-p125 :027 > records.first['title']
 => "Chinese electronics industry now uses unique Danish robots"
1.9.3-p125 :028 > records.first['linkID']
 => "/watch?v=hjkVdFa3DH8"

#11 楼 @brucebot 那就是你给我们发的原始的那个字符串有问题~ 去掉 first 就好了

#12 楼 @zj0713001 多谢你的建议,最终搞定了,数据成功导入

需要 登录 后方可回复, 如果你还没有账号请 注册新账号