Ruby on Rails Monday, December 15, 2014

have you tried #as_json?

On Monday, December 15, 2014 9:59:02 PM UTC+2, Ruby-Forum.com User wrote:
Hi,


I have around 2000 objects to render. Unfortunately the view rendering
with jbuilder takes very long time, although the objects are small like
this:


center: {lat: 48.8130685274225, lon: 10.0405494495457}
lat: 48.8130685274225
lon: 10.0405494495457
n: "Aalen/Hirtenteich"
st: null
sy: null


The result query I pass to the view looks like this
@resorts   = Resort.where(:id => resort_ids).includes(:snow_reports)

A resort has many snow_reports. st and sy are fields from snow reports.

the view looks like this:

   json.array!(@resorts) do |resort|
  json.cache!("resort_light_#{resort.id}") do

    #json.partial! 'json_partials/snow_in_resort', resort: resort
    json.n resort.name
    json.sy resort.try(:snow_reports).last.try(:snow_valley) if
resort.snow_reports
    json.st resort.try(:snow_reports).last.try(:snow_summit) if
resort.snow_reports
    json.center do
        json.lat resort.centroid.lat
        json.lon resort.centroid.lon
    end
  end
end


  Rendered resorts/find.json.jbuilder (1649.0ms)
Completed 200 OK in 1889ms (Views: 1593.1ms | ActiveRecord: 77.8ms |
Solr: 0.0ms)
In development mode the request takes 5s in chrome to run.

The first thing I ve done to speed up is changing the json gem to

      gem 'oj'
      gem 'oj_mimic_json'

It speeds up slightly but not enough. I also tried active model
serialization wich was about the same time, so I prefer to stick to
jbuilder since it is convenient to assemble json objects.
So how can I speed up rendering?

Help is highly appreciated since I am stuck for a week now.

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f52b6342-c938-42d1-8cc0-2678adbd3197%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment