static public Method

Base.count_by_sql(sql)

Contents:

Returns the result of an SQL statement that should only include a COUNT(*) in the SELECT part. The use of this method should be restricted to complicated SQL queries that can’t be executed using the ActiveRecord::Calculations class methods. Look into those before using this.

Options

sql: An SQL statement which should return a count query from the database, see the example below

Examples

Product.count_by_sql "SELECT COUNT(*) FROM sales s, customers c WHERE s.customer_id = c.id"

Source Code

# File active_record/base.rb, line 729
def count_by_sql(sql)
  sql = sanitize_conditions(sql)
  connection.select_value(sql, "#{name} Count").to_i
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.