在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]