将ActiveRecord数组转换为字符串

时间:2014-05-05 22:48:01

标签: ruby-on-rails ruby rails-activerecord

我使用ActiveRecord获取帐户值,但我无法正确格式化...

Account.all.map { |m| [m.day, m.NetLiquidation] }.unshift(['Day', 'value']).join(",") 

给了我

[["Day", "value"], ["04/09/14", "94465.80"],["04/10/14", "96403.90"]]

我需要的是一个像这样的字符串...

"Day,value\n" + "04/09/14, 97450.57\n" + "04/10/14, 96403.90\n"

我知道我已经接近了,但是任何红宝石巫师都知道快捷捷径吗?

1 个答案:

答案 0 :(得分:0)

这应该有效:

Account.all.map { |m| [m.day, m.NetLiquidation] }.unshift(['Day', 'value']).map(|x| x.join(",")}.flatten.join("\n")