Ruby on rails 3 Ruby-无法读取yaml文件
大家好,我正在从数据库中检索数据并将其存储在文件中。我正在以yaml格式存储数据Ruby on rails 3 Ruby-无法读取yaml文件,ruby-on-rails-3,yaml,file-handling,Ruby On Rails 3,Yaml,File Handling,大家好,我正在从数据库中检索数据并将其存储在文件中。我正在以yaml格式存储数据 require 'mysql2' require 'yaml' client = Mysql2::Client.new(:host => "localhost",:username => 'root' , :password => 'root' , :database => 'jgroup') results = client.query("SELECT * FROM ji
require 'mysql2'
require 'yaml'
client = Mysql2::Client.new(:host => "localhost",:username => 'root' , :password => 'root' , :database => 'jgroup')
results = client.query("SELECT * FROM jivegroup")
File.open("D:/j.yml","w") do |file|
results.each do |index|
file.write(index.to_yaml);
end
end
below is my file "j.yml"
---
groupID: 1000
name: T1
description: ""
creationDate: 1209446456903
modificationDate: 1378128624533
---
groupID: 1001
name: T2
description:
creationDate: 1209446473683
modificationDate: 1378181717000
---
但是当我试图用YAML::load加载上面的文件时,它给出了我唯一的第一条记录。我想加载所有记录,请提供帮助。
下面是我读取yml文件的代码
YAML::load( File.read('D:/jivegroup.yml') )
{"groupID"=>1000, "name"=>"T1", "description"=>"", "creationDate"=>1209446456903, "modificationDate"=>1378128624533}
如果您确实希望使用此文件设计,那么请使用
YAML::load_documents
一次加载多条记录
我建议改为在文件中使用列表。对我来说,它似乎更清晰(因为您拥有的是一个类似记录的列表,而不是一组不相关的文档)。他们终于找到了关于堆栈溢出本身的我自己问题的答案。我使用YAML.load\u stream(open(“D:/jive\u group.yml”)代替YAML::load(File.read('D:/jivegroup.yml'))