使用Active Model Serializer或JSON渲染sum属性

时间:2016-02-26 19:06:13

标签: ruby-on-rails json activerecord active-model-serializers

我的默认代码是Lab.all,当我只是像Lab.all.to_json那样呈现完美的json

{"id":1,"lab_id":1,"test_id":2,"price_cents":20000,"duration":null,"created_at":"2016-02-26T14:40:40.825Z","updated_at":"2016-02-26T14:40:40.825Z"}

我有以下范围:

@lab_tests = LabTest.group(:lab).sum(:price_cents)并以json:

返回以下内容
{"#\u003cLab:0x007f8e0a44afa8\u003e":20000}

如何让它像这样渲染json:

  {"id":1,"lab_id":1,"test_id":2,"price_cents":20000,"duration":null,"created_at":"2016-02-26T14:40:40.825Z","updated_at":"2016-02-26T14:40:40.825Z", "price_cents":200000 }

我尝试在我的范围中添加.select参数,但即使这样也不会改变任何内容。

0 个答案:

没有答案