def size(collection)
      count = (collection.size rescue nil)

      case count
      when Integer
        count
      when Hash
        count.size  # This represents the number of displayed datatable rows, not the sum all groups (which might be more)
      else
        if collection.klass.connection.respond_to?(:unprepared_statement)
          collection_sql = collection.klass.connection.unprepared_statement { collection.to_sql }
          (collection.klass.connection.exec_query("SELECT COUNT(*) FROM (#{collection_sql}) AS datatables_total_count").rows[0][0] rescue 1)
        else
          (collection.klass.connection.exec_query("SELECT COUNT(*) FROM (#{collection.to_sql}) AS datatables_total_count").rows[0][0] rescue 1)
        end.to_i
      end
    end
def size(collection)
      count = (collection.size rescue nil)

      case count
      when Integer
        count
      when Hash
        count.size  
      else
        if collection.klass.connection.respond_to?(:unprepared_statement)
          collection_sql = collection.klass.connection.unprepared_statement { collection.to_sql }
          (collection.klass.connection.exec_query("SELECT COUNT(*) FROM (
        else
          (collection.klass.connection.exec_query("SELECT COUNT(*) FROM (
        end.to_i
      end
    end
def size ( collection ) count = ( collection . size rescue nil ) case count when Integer count when Hash count . size # This represents the number of displayed datatable rows, not the sum all groups (which might be more) else if collection . klass . connection . respond_to? ( :unprepared_statement ) collection_sql = collection . klass . connection . unprepared_statement { collection . to_sql } ( collection . klass . connection . exec_query ( "SELECT COUNT(*) FROM (#{collection_sql}) AS datatables_total_count" ) . rows [ 0 ] [ 0 ] rescue 1 ) else ( collection . klass . connection . exec_query ( "SELECT COUNT(*) FROM (#{collection.to_sql}) AS datatables_total_count" ) . rows [ 0 ] [ 0 ] rescue 1 ) end . to_i end end
Not every ActiveRecord query will work when calling the simple .count
 Custom selects:
   User.select(:email, :first_name).count will throw an error
 Grouped Queries:
   User.all.group(:email).count will return a Hash
Not every ActiveRecord query will work when calling the simple . count Custom selects : User . select ( : email : first_name ) . count will throw an error Grouped Queries : User . all . group ( : email ) . count will return a Hash