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

Ticket #10254: activerecord_calculations_fix.diff

File activerecord_calculations_fix.diff, 2.3 kB (added by bloopletech, 10 months ago)

Updated patch

  • 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 
     
    2424      # * <tt>:group</tt>: An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause. 
    2525      # * <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 
    2626      #   include the joined columns. 
     27      # * <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 
     28      #   of a database view). 
    2729      # * <tt>:distinct</tt>: Set this to true to make this a distinct calculation, such as SELECT COUNT(DISTINCT posts.id) ... 
    2830      # 
    2931      # Examples for counting all: 
     
    102104      # * <tt>:group</tt>: An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause. 
    103105      # * <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 
    104106      #   include the joined columns. 
     107      # * <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 
     108      #   of a database view). 
    105109      # * <tt>:distinct</tt>: Set this to true to make this a distinct calculation, such as SELECT COUNT(DISTINCT posts.id) ... 
    106110      # 
    107111      # Examples: 
     
    178182           
    179183          sql << ", #{options[:group_field]} AS #{options[:group_alias]}" if options[:group] 
    180184          sql << " FROM (SELECT DISTINCT #{column_name}" if use_workaround 
    181           sql << " FROM #{table_name} " 
     185          sql << " FROM #{options[:from] || table_name} " 
    182186          if merged_includes.any? 
    183187            join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, merged_includes, options[:joins]) 
    184188            sql << join_dependency.join_associations.collect{|join| join.association_join }.join