render :json mailbox.inbox.as_json(:include => [
{:messages => {:only => [:body]}},
{:recipients => {:only => [:Firstname,:Surname]}}
])
从我的 Rails 控制器产生以下输出:
[
{
"conversation":
{
"subject":"Subject for conversation 2",
"messages" :[
{"body":"Message 1"},
{"body":"Message 2"}
],
"recipients": [
{"Firstname":"James","Surname":"Smith"},
{"Firstname":"John","Surname":"Doe"}
]
}
}
]
我想做的是向每个对话对象添加一个current_user键:值对,因此输出示例如下所示:
[
{
"conversation":
{
"subject":"Subject for conversation 2",
"messages" :[
{"body":"Message 1"},
{"body":"Message 2"},
],
"recipients": [
{"Firstname":"James","Surname":"Smith"},
{"Firstname":"John","Surname":"Doe"}
],
"current_user" :[
{"id":10}
]
}
}
]
我无权访问有问题的对话模型(它正在使用邮箱 gem),因此无法覆盖 as_json。任何帮助,将不胜感激