17andLearning
17andLearning

Reputation: 477

RoR: Filter generated JSON to certain parameters

I'm generating JSON as follows:

def suggest_gems
  @allgems = RubyGem.where("name like ?", "%#{params[:q]}%")
  respond_to do |format|
  format.json { render :json => @allgems.to_json }
  end
end 

However, the RubyGem table is quite large, with values for descriptions, links and created, updated at. How can I only output the name and id in the generated JSON to decrease loading times? Thanks.

Upvotes: 1

Views: 307

Answers (3)

John H
John H

Reputation: 2488

You can really clean up your controller method there and achieve what you want like this:

def suggest_gems
  @allgems = RubyGem.where("name like ?", "%#{params[:q]}%")
  respond_with(@allgems, :only => [:id, :name])
end 

Provided you have this at the top of your controller:

respond_to :json

EDIT

If you don't want to use respond_to then do this:

def suggest_gems
  @allgems = RubyGem.where("name like ?", "%#{params[:q]}%")
  render :json => @allgems.as_json(:only => [:id, :name])
end 

Upvotes: 2

17andLearning
17andLearning

Reputation: 477

So I figured how to do that without changing the as_json method or using the respond_with statement, by adding a few lines of code. Here's what I did:

def suggest_gems (result = [])
   @allgems = RubyGem.where("name like ?", "%#{params[:q]}%")
   @allgems.each { |g|
     result << { :id => g.id, :name => g.name }
     } 
   respond_to do |format|
   format.json { render :json => result.to_json }
   end
end 

I basically defined an array and associated the name and id attributes accordingly, then formatted the array as JSON. Shaved around 6 seconds off the loading time :)

Adding this answer here so that others might find it helpful!

Upvotes: 0

Khaled
Khaled

Reputation: 2091

You need to override as_json in your model to return only the attributed you want to send back.

def as_json(options)
  super(only: [:id, :name])
end

Upvotes: 0

Related Questions