Hommer Smith Hommer Smith - 1 year ago 80
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 Source

Something like this should work:

render :json => @programs, :include => {:insurer => {:only => :name}}, :except => [:created_at, :updated_at]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download