我有一个集合中的记录:
{ "_id" : ObjectId("4f0224ad6f85ce027e000031"), "monday" : "7am" }
{ "_id" : ObjectId("4f0224ad6f85ce027e00002e"), "tuesday" : "11.40am" }
{ "_id" : ObjectId("4f0224ad6f85ce027e000025"), "wednestay" : "12am",
"thursday" : "1pm" }
在控制器中,我将抓取所有项目,在视图中我想以形状打印它们:
monday 7am
tuesday 11.40am
wednesday 12am thursday 1pm
我的应用程序正在Rails上运行。存在任何快速&优雅的方式来做到这一点?谢谢!
修改 这对我有用:
records = collection.where('something' => variable)
records.each do |rec|
puts rec._id
end
这不是
records = collection.where('something' => variable)
records.each do |rec|
rec.each do |k, v| #here is the error "undefined each"
next if k == '_id' # skip the _id field
puts "#{k} #{v}"
end
end
答案 0 :(得分:1)
你基本上不需要做任何事情。只需加载所有记录,循环播放,然后打印。
records = collection.find()
records.each do |rec|
rec.attributes.each do |k, v|
next if k == '_id' # skip the _id field
puts "#{k} #{v}"
end
end