Ruby on rails 如何使用我拥有的数据库表从rabl gem和money gem生成json响应?

Ruby on rails 如何使用我拥有的数据库表从rabl gem和money gem生成json响应?,ruby-on-rails,ruby,json,currency,rabl,Ruby On Rails,Ruby,Json,Currency,Rabl,这是我想从rablgem构建的json [ "M-0": {"revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"}, "M-1": {"revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"}, "M-2": {"revenue":"v

这是我想从rablgem构建的json

[
"M-0": {"revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"},
"M-1": {"revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"},
"M-2": {"revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"},
"M-3": ["revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"],
"M-4": {"revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"},
"M-5": {"revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"}
 ]
这是我的数据库表

create_table "merchant_monthly_stats", force: true do |t|
    t.integer  "created_count",        default: 0
    t.integer  "ready_count",          default: 0
    t.integer  "paid_count",           default: 0
    t.integer  "partially_paid_count", default: 0
    t.integer  "cancelled_count",      default: 0
    t.integer  "failed_count",         default: 0
    t.integer  "processed_count",      default: 0
    t.integer  "expired_count",        default: 0
    t.integer  "merchant_id",                          null: false
    t.integer  "year",                                 null: false
    t.integer  "month",                                null: false
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "overdue_count",        default: 0
    t.integer  "fee_centimes",         default: 0,     null: false
    t.string   "fee_currency",         default: "XAF", null: false
    t.integer  "revenue_centimes",     default: 0,     null: false
    t.string   "revenue_currency",     default: "XAF", null: false
  end
这是我的控制器方法

def history
    current_month = Time.now.month
    current_year = Time.now.year
    list_of_last_six_months = [current_month]
    5.times do |i|
      list_of_last_six_months << (current_month - i )
    end 

    month_array =[]
    list_of_last_six_months.each { |month|
      record = MerchantMonthlyStat.find_by merchant_id: current_api_user.id, year: current_year, month: month
      m = { 
        :revenue => Money.new(record.revenue_centimes , record.revenue_currency).format,
        :fees => Money.new(record.fee_centimes,record.fee_currency).format, 
        :created => record.created_count , 
        :paid => record.paid_count , 
        :partially_paid => record.partially_paid_count }
      month_array.push(m)
    }
    respond_with(month_array, status: :ok, template: 'api/merchant/mobiles/history')
  end
Rabl文档为此编写了以下Rabl(这对我来说毫无意义):


但是我不知道如何应用它以rabl和我想要的格式获取我的month_数组?有人想解决这个问题吗?我被卡住了

只需使用
render:json

render json: month_array
无需修改模板,您已经在controller中构建了阵列


从评论更新:使month_数组实例变为VARABLE–将my_数组更改为@my_数组。然后在您的rabl模板中,只需编写collection@my_array,object_root:false

,但您提供的看起来不像JSON。这些
M-0
M-1
等标签是什么?我看不出有什么效果。你们的问题是为我写代码。那个么你们认为上面剩下的东西是谁写的呢?他们不是努力吗?我只是不太了解rails。我从一个星期开始学习,这就是我所能取得的进步。就这些。@EugZol我已经在中更正了jsonquestion@samo有大量的文档、精彩的维基页面和许多web示例。如果你尝试的话,这是可以做到的。事实上,我已经向我的前辈提出了这个建议,因为我很容易做到。但他说我们正在使用rabl,我需要学习它。否则我会把你标记为答案(好吧,你可以让month_array实例变为VARABLE–将
my_array
更改为
@my_array
。然后在你的rabl模板中只需编写
collection@my_array,object_root:false
。欢迎你。如果你的上级要求你重构代码,将更多的东西从你的控制器移动到rabl,请随意询问其他问题。)我们也会想出一些办法:)如果你喜欢的话,考虑一下我的回答。但是我已经把它标记为答案。因为那是最有帮助的。你能更新你的答案吗?谢谢Eugzol。这也帮助我学习了rabl语法,以防有人想更好地理解
# app/views/posts/index.rabl
collection @posts
attributes :id, :title, :subject
child(:user) { attributes :full_name }
node(:read) { |post| post.read_by?(@user) }
render json: month_array