【问题标题】:Rails rabl set calculated fieldRails rabl 设置计算字段
【发布时间】:2013-04-10 17:09:41
【问题描述】:

我需要渲染一些 json 数据。为此,我使用 rabl....

我在 index.rabl 中有这样的代码:

collection @banks, :root => "bank", :object_root => false
attributes :id, :central_office_address, :location_id, :name, :year_of_foundation

它为我生成了 json....
但现在我还需要为每个对象条目添加一些计算字段。

例如新字段(不在模型中)::exch_count 并为它做类似(伪)::exch_count @banks[i].exchangers.count * 3

但是我如何在 ruby​​ on rails + rabls 中做到这一点?

【问题讨论】:

    标签: ruby-on-rails json rabl


    【解决方案1】:

    你可以向node 传递一个使用银行作为参数的块:

    node(:exch_count) {|bank| bank.exchangers.count * 3 }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多