使用rabl gem收集的重复项目

时间:2019-07-09 09:37:23

标签: ruby ruby-on-rails-4 rabl

我有一组带有一系列属性阅读器的对象帖子:

# app/app.rb
get "/posts", :provides => [:json, :xml] do
  @user = current_user
  @posts = Post.order("id DESC")
  render "posts/index"
end

# app/views/posts/index.rabl
collection @posts
attributes :id, :title, :subject
child(:user) { attributes :full_name }

node(:readers) { |post| post.readers }

我明白了:

[{  "post" :
  {
    "id" : 5, "title": "...", "subject": "...",
    "user" : { "full_name" : "..." },
    "readers" : ["Pepito", "Pedro", "Manolo"]
  }
}]

但是我想得到这个:

[{  "post" :
  {
    "id" : 5, "title": "...", "subject": "...",
    "user" : { "full_name" : "..." },
    "readers" : "Pedro"
  }
},
 {  "post" :
  {
    "id" : 5, "title": "...", "subject": "...",
    "user" : { "full_name" : "..." },
    "reader" : "Manolo"
  }
},
{  "post" :
  {
    "id" : 5, "title": "...", "subject": "...",
    "user" : { "full_name" : "..." },
    "reader" : "Pedro"
  }
}]

有可能在rabl模板中实现此目标。或者我需要先提供该收藏。

0 个答案:

没有答案