Hommer Smith
Hommer Smith

Reputation: 27852

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?

Upvotes: 56

Views: 47948

Answers (6)

user3716986
user3716986

Reputation: 1

#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"}}
  ]

Upvotes: 0

Arenzel
Arenzel

Reputation: 1156

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  

Upvotes: 9

Sean M
Sean M

Reputation: 2030

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

Upvotes: 20

IliasT
IliasT

Reputation: 4301

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.

Upvotes: 4

Abram
Abram

Reputation: 41874

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

Upvotes: 13

Nobita
Nobita

Reputation: 23713

Something like this should work:

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

Upvotes: 93

Related Questions