如何在json渲染中获取完整的belongs_to对象? [英] How to get full belongs_to object in json render?
本文介绍了如何在json渲染中获取完整的belongs_to对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基本上,我有一个对象belongs_to :companies,并且有:company_id 属性.当我呈现 json: @coupons 时,JSON 是否可能包含其所有者的属性而不是 company_id?
Basically, I have an object that belongs_to :companies, and has the :company_id attribute. When I render json: @coupons, is it possible for the JSON to contain an attribute of its owner rather than the company_id?
推荐答案
你也许可以做一些类似 render :json =>@coupons.to_json(:include => :company)
,至少它似乎与我在 rails 2.3.8 中的初始测试一起工作.
You might be able to do something like render :json => @coupons.to_json(:include => :company)
, at least it seems to have worked with my initial testing in rails 2.3.8.
已编辑答案以使用 :include =>:company
而不是 :include =>:公司
这篇关于如何在json渲染中获取完整的belongs_to对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文