我有一组带有一系列属性阅读器的对象帖子:
# 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模板中实现此目标。或者我需要先提供该收藏。