在json对象中的rabl包裹集合

时间:2012-10-20 20:07:18

标签: ruby-on-rails json rabl

我正在使用RABL生成JSON响应。我无法实现以下结构:

{
  "articles": [
     {
        "created_at": "2012-10-20T15:57:31Z",
        "description": "MK DEsc",
        "id": 1,
        "title": "MK Title",
        "updated_at": "2012-10-20T15:58:14Z",
         media:[{
            id : 1,
            title : "title"
        }]
     },
     {
        "created_at": "2012-10-20T16:38:24Z",
        "description": "fdsfdffffffff",
        "id": 2,
        "title": "asdads",
        "updated_at": "2012-10-20T16:38:24Z",
         media:[{
            id : 1,
            title : "title"
        }]
     }
   ]
}

我有 @artices 对象,其中包含媒体数组。我有rabl配置设置,如:

   config.include_json_root = false
   config.include_child_root = false
到目前为止,我的.rabl模板看起来像这样:

object false
node :articles do
  @articles.each do |article|
  end
end

我无法在此结构中添加媒体信息。

感谢任何帮助

1 个答案:

答案 0 :(得分:2)

我设法这样做了:

collection @articles, :root => "articles", :object_root => false
attributes *Article.column_names

child(:media) do
  attributes *Medium.column_names
end