在Rails中呈现JSON时包括关联的模型 [英] Include associated model when rendering JSON in Rails

查看:75
本文介绍了在Rails中呈现JSON时包括关联的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有这行:

 render json: @programs, :except => [:created_at, :updated_at]

但是,由于程序属于公司,我想显示公司名称而不是公司ID.

However, since a Program belongs_to a Company I would like to show the Company name instead of the Company Id.

在渲染程序时如何包含公司名称?

How can I include the company name when rendering Programs?

推荐答案

类似的方法应该起作用:

Something like this should work:

render :json => @programs, :include => {:insurer => {:only => :name}}, :except => [:created_at, :updated_at]

这篇关于在Rails中呈现JSON时包括关联的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆