作为JSON结果的一部分,返回基于ID的扩展记录

|| 我有一个Rails服务器将数据作为JSON返回到我的移动应用程序,如下所示, 在控制器中
    format.json { render :json => @game }
哪个返回,
    {\"game\":{\"created_at\":\"Thu, 14 Apr 2011 11:14\",\"title\":\"Banana throwing\",\"updated_at\":\"Thu, 14 Apr 2011 11:16\",\"id\":5,\"day\":\"2011-07-14\",\"location_id\":5}}
返回的记录包括从\'game \'表到另一个表\'location \'的引用,尤其是该表中ID为5的条目。 现在,我的移动应用程序可能会遇到麻烦,以解决另一个表中的location_id,但是如果最初返回的JSON已经解析了location_id值并包含扩展的\'location,则将更加简单明了记录在原始结果中,例如,
{\"game\":{\"created_at\":\"Thu, 14 Apr 2011 11:14\",\"title\":\"Banana throwing\",\"updated_at\":\"Thu, 14 Apr 2011 11:16\",\"id\":5,\"day\":\"2011-07-14\",\"location\":{\"place\":\"Buckingham Palace\"}}}
...位置表中ID为5的条目已扩展为嵌套JSON。 是否可以在服务器端执行此操作? 任何帮助,将不胜感激。 谢谢。
已邀请:
尝试:
format.json { render :json => @person.to_json(:include => [:location]) }
抱歉,敬请原谅,但
to_json
:include
差不多。我现在还没有在控制器中对其进行测试,但是可以在控制台上使用它,这还可以。

要回复问题请先登录注册