Hommer Smith Hommer Smith - 1 month ago 6
JSON Question

Include associated model when rendering JSON in Rails

Right now I have this line:

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


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?

Answer

Something like this should work:

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