在 Rails 中渲染 JSON 时包含关联模型 [英] Include associated model when rendering JSON in Rails

查看:27
本文介绍了在 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?

推荐答案

这样的事情应该可行:

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

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

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