【发布时间】:2014-02-08 19:48:17
【问题描述】:
我有一个 JSON 字符串数组。如何将它们转换为 Activerecord 模型数组?
我现在的代码是这样的,我就不一一迭代了:
jsons = ['{"id": 1, "field1" : "value1"}'] #this is an array of jsons
models = [] #i want an array of models back
jsons.each do |json|
if(json == nil)
next
end
begin
hash = JSON.parse(json)
rescue
next
end
model = className.new
model.attributes = hash
model.id = hash["id"]
models << model
end
【问题讨论】:
-
数据既不是有效的 JSON 也不是 Ruby Hash。
标签: ruby-on-rails ruby json activerecord