module ActiveRecord::Querying
def count_by_sql(sql)
==== Parameters
# => 12
Product.count_by_sql "SELECT COUNT(*) FROM sales s, customers c WHERE s.customer_id = c.id"
database engines.
as it could lock you into a specific database engine or require a code change to switch
using the ActiveRecord::Calculations class methods. Look into those before using this method,
The use of this method should be restricted to complicated SQL queries that can't be executed
Returns the result of an SQL statement that should only include a COUNT(*) in the SELECT part.
def count_by_sql(sql) with_connection do |c| c.select_value(sanitize_sql(sql), "#{name} Count").to_i end end