Add a workaround for the ActiveRecord count bug

See https://github.com/rails/rails/issues/9458 for details.