Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10254: activerecord_calculations_from_(fix).diff

File activerecord_calculations_from_(fix).diff, 2.5 kB (added by michaelboutros, 8 months ago)
  • activerecord/lib/active_record/calculations.rb

    old new  
    11module ActiveRecord 
    22  module Calculations #:nodoc: 
    3     CALCULATIONS_OPTIONS = [:conditions, :joins, :order, :select, :group, :having, :distinct, :limit, :offset, :include
     3    CALCULATIONS_OPTIONS = [:conditions, :joins, :order, :select, :group, :having, :distinct, :limit, :offset, :include, :from
    44    def self.included(base) 
    55      base.extend(ClassMethods) 
    66    end 
     
    2626      # * <tt>:group</tt>: An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause. 
    2727      # * <tt>:select</tt>: By default, this is * as in SELECT * FROM, but can be changed if you, for example, want to do a join but not 
    2828      #   include the joined columns. 
     29      # * <tt>:from</tt>: By default, this is the table name of the class, but can be changed to an alternate table name (or even the name  
     30      #   of a database view). 
    2931      # * <tt>:distinct</tt>: Set this to true to make this a distinct calculation, such as SELECT COUNT(DISTINCT posts.id) ... 
    3032      # 
    3133      # Examples for counting all: 
     
    104106      # * <tt>:group</tt> - An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause. 
    105107      # * <tt>:select</tt> - By default, this is * as in SELECT * FROM, but can be changed if you for example want to do a join, but not 
    106108      #   include the joined columns. 
     109      # * <tt>:from</tt>: By default, this is the table name of the class, but can be changed to an alternate table name (or even the name  
     110      #   of a database view). 
    107111      # * <tt>:distinct</tt> - Set this to true to make this a distinct calculation, such as SELECT COUNT(DISTINCT posts.id) ... 
    108112      # 
    109113      # Examples: 
     
    174178 
    175179          sql << ", #{options[:group_field]} AS #{options[:group_alias]}" if options[:group] 
    176180          sql << " FROM (SELECT DISTINCT #{column_name}" if use_workaround 
    177           sql << " FROM #{connection.quote_table_name(table_name)} " 
     181          sql << " FROM #{options[:from] || connection.quote_table_name(table_name)} " 
    178182          if merged_includes.any? 
    179183            join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, merged_includes, options[:joins]) 
    180184            sql << join_dependency.join_associations.collect{|join| join.association_join }.join