Question

Every rails etag example I've seen has been very simple with the fresh_when method being called as the last line in the controller. I'm trying to understand how the fresh_when method works for a controller that has resource intensive method calls that I don't want to call in the request if the page is still fresh. For example,

class NotesController < ApplicationController
  etag { current_user.try :id }

  def show
    @note = Note.find(params[:id])
    @note.do_some_expenisve_data_manipulation
    fresh_when(@note)
  end
end

Will @note.do_some_expensive_data_manipulation be called if the note hasn't been updated since the last time this user updated it? If I placed that line below the fresh_when would it be called? Thanks!

Was it helpful?

Solution

I feel like using stale? is easier for me to understand then fresh_when. Here is an example.

if stale?(etag: @note, last_modified: @note.updated_at)
  respond_to do |format|
    format.html {
      // do expensive work here
    }
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top