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 键:值对,以便out示例如下所示:
[
{
"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。任何帮助将不胜感激