我有一个需要输出自定义 json 响应的 rails 应用程序。
我有一个Post
模型,其中有很多statuses
. 的status
apost
对于每个user
.
我想以Post.all
json 的形式返回,其中每个post
包含与用户相关的status
es 作为常规属性(看起来它们属于的属性post
,没有嵌套)
我该怎么做?我不想将 current_user 方法传递给我的模型,也不想使用序列化程序。
无论如何我可以做这样的事情:
respond_with(posts: @posts.as_json(:methods => [:status(current_user)])
#NOTE the current user arg
编辑:
澄清,我想要:
id: 1, content: 'this is a post', status: 'reviewed'
#as json of course
任何帮助表示赞赏。