Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using the stale method on collections possible?

Is it possible to use stale? with a collection? For example, I'm developing a REST api that will allow the client to occasionally query the server to ask for the most up-to-date list of items. It would be nice if stale? could check a collection against my If-Modified.. header and send a 304 if nothing has changed.

Quick example:

def index
  @items = Item.all
  if stale?(@items)
    render json: @items
  end
end

def show
  if stale?(@item)
    render json: @item
  end
end
like image 331
Parazuce Avatar asked Nov 14 '13 04:11

Parazuce


1 Answers

For Rails < 5:

stale?(last_modified: @items.maximum(:updated_at))

For Rails >= 5 just:

stale?(@items)
like image 101
Artem P Avatar answered Oct 03 '22 23:10

Artem P