I’ve learned how to use the Rails.cache.fetch function to cache database calls, however, when I respond to the request, I include the contents of several related tables, for example
tracks = Rails.cache.fetch("events-by-track", :expires_in => 12.hours) do
Track.find(:all)
end
respond_with(tracks, :include => {:events => {:include => :speaker}})
This caches (I think) the call Track.find(:all), but is it also caching the included events and speaker?
No, it is not caching those. You’ll have to put those inside the call to include them as well. For example, something like this:
tracks = Rails.cache.fetch("events-by-track", :expires_in => 12.hours) do Track.find(:all, :include => {:events => :speaker}) end respond_with(tracks)Example of array of associated objects:
tracks, events = Rails.cache.fetch("events-by-track", :expires_in => 12.hours) do tracks = Track.find(:all, :include => [:events]) events = tracks.collect(&:events) tracks, events end respond_with(tracks)