RailsCasts Pro episodes are now free!

Learn more or hide this

Will Koehler's Profile

GitHub User: willkoehler

Comments by Will Koehler

Avatar

You are correct, update_all is called once for each faq item, which results in a separate query for each faq item. The performance gain is because update_all updates the order field in a single query as opposed to first loading the model then doing an update in a separate query.

This still works in Rails 4 by the way, but the syntax is a little different:

ruby
Faq.where(id: id).update_all(position: index+1)
Avatar

FYI, If you're trying this in your own app and not seeing the etag headers, it could be because MiniProfiler (described in episode #368) strips the etag headers and rewrites the cache-control header. See my comment in that episode: http://railscasts.com/episodes/368-miniprofiler?view=comments#comment_159804

Avatar

I was trying the caching techniques from episode #321 and pulling my hair out trying to figure out why I wasn't get etag headers in my responses. Turns out that MiniProfiler strips the etag headers. It also rewrites the Cache-Control header.

From MiniProfiler source: mini-profiler/profiler.rb

ruby
# mini profiler is meddling with stuff, we can not cache cause we will get incorrect data
# Rack::ETag has already inserted some nonesense in the chain
headers.delete('ETag')
headers.delete('Date')
headers['Cache-Control'] = 'must-revalidate, private, max-age=0'
[status, headers, body]

Probably isn't a big deal because the profiler is disabled in production. But makes things trickier on the development side. Just wanted to share this in case anyone hits the same issue.

Avatar

In case it helps anyone else, here is a jQuery version of endless_page.js The jQuery version fixes the compatibility problem with IE as well.

var currentPage = 1

function checkScroll() {
  if (nearBottomOfPage()) {
    currentPage ++;
    $.ajax(window.location.pathname + '.js?page=' + currentPage )
  } else {
    setTimeout("checkScroll()", 250);
  }
}

function nearBottomOfPage() {
  return scrollDistanceFromBottom() < 150;
}

function scrollDistanceFromBottom(argument) {
  return $(document).height() - ($(window).height() + $(window).scrollTop());
}