我在玩示例数据。我目前在数据库中没有任何数据。
{
"Name": "john",
"ProductIds: [1, 2, 3],
"Products": [
{
"description": [
{
"a": "abc"
},
{
"b": "def"
}
]
}
]
}
例如,当我们这样做时Customer.all
,得到的结果如下:
[#<Customer:0x00007fd75542f250
id: "123",
customer_name: "sam",
city: "Houston",
state: "Texas"]
我在模型中有此数据,并且我正在尝试编写一种方法来将该示例数据作为对象返回。
任何帮助表示赞赏。
使用结构。
class Customer
def self.all(collection)
customer = Struct.new(:name, :address)
collection.map {|e| customer.new(e[:name], e[:address])}
end
end
collection = [
{name: "pepe", address: "abc"},
{name: "ciro"}
]
Customer.all(collection) #=> [
#<struct name="pepe", address="abc">,
#<struct name="ciro", address=nil>
]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句