Include associated model when rendering JSON in Rails

Ruby on-RailsJson

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Something like this should work:

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

Solution 2 - Ruby on-Rails

i was getting the same "can't clone Symbol file" error while rendering json with includes from a controller method. avoided it like so:

render :json => @list.to_json( :include => [:tasks] )

Solution 3 - Ruby on-Rails

You can also do this at the model level.

program.rb

  def as_json(options={})
    super(:except => [:created_at, :updated_at]
          :include => {
            :company => {:only => [:name]}
          }
    )
  end
end

Now in your controller:

render json: @programs

Solution 4 - Ruby on-Rails

Consider using jbuilder to include nested models in a maintainable way:

# /views/shops/index.json.jbuilder
json.shops @shops do |shop|

  # shop attributes to json
  json.id shop.id
  json.address shop.address

  # Nested products
  json.products shop.products do |product|
    json.name product.name
	json.price product.price
  end

end  

Solution 5 - Ruby on-Rails

Try this. Ref

#`includes` caches all the companies for each program (eager loading)
programs = Program.includes(:company)

#`.as_json` creates a hash containing all programs
#`include` adds a key `company` to each program
#and sets the value as an array of the program's companies
#Note: you can exclude certain fields with `only` or `except`
render json: programs.as_json(include: :company, only: [:name])

Also, no need to make @programs an instance variable, as I'm assuming we are not passing it to a view.

Solution 6 - Ruby on-Rails

#includes is used to avoid n+1 query.
# http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations
Here is an example for the above example.Lets say you have posts and each post has many comments to it.

  @posts = Post.where('id IN [1,2,3,4]').includes(:comments)
  respond_to do |format|
     format.json {render json: @posts.to_json(:include => [:comments]) }
  end

  #output data
  [
    {id:1,name:"post1",comments:{user_id:1,message:"nice"}}
    {id:2,name:"post2",comments:{user_id:2,message:"okok"}}
     {id:3,name:"post1",comments:{user_id:12,message:"great"}}
    {id:4,name:"post1",comments:{user_id:45,message:"good enough"}}
  ]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHommer SmithView Question on Stackoverflow
Solution 1 - Ruby on-RailsNobitaView Answer on Stackoverflow
Solution 2 - Ruby on-RailsSean MView Answer on Stackoverflow
Solution 3 - Ruby on-RailsAbramView Answer on Stackoverflow
Solution 4 - Ruby on-RailsArenzelView Answer on Stackoverflow
Solution 5 - Ruby on-RailsIliasTView Answer on Stackoverflow
Solution 6 - Ruby on-Railsuser3716986View Answer on Stackoverflow