如何使用JBuilder Rails在同一个JSON对象中编码2个对象

时间:2013-12-02 07:41:35

标签: ruby-on-rails jbuilder

使用JBuilder有以下代码:

  json.details   place.details

  if (place.type == 'restaurant')
    json.food_types place.details.food_types, :id
  end

JSON回复:

details":{"average_check":100,"id":12},"food_types":[{"id":1}]}

现在我需要将food_types移动到细节:

details":{"average_check":100,"id":12", food_types":[{"id":1}]}}

我该怎么做?

1 个答案:

答案 0 :(得分:1)

你可以像这样构建它

json.details do 
    json.(place.details, :average_check, :id)
    if (place.type == 'restaurant')
       json.food_types place.details.food_types, :id
    end
  end
end

Jbuilder objects can be directly nested inside each other. Useful for composing objects.