diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 4c23d2f8e2..b1ae2fbe78 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -899,7 +899,7 @@ module ActiveRecord #:nodoc: # Both calls delete the affected posts all at once with a single DELETE statement. If you need to destroy dependent # associations or call your before_* or +after_destroy+ callbacks, use the +destroy_all+ method instead. def delete_all(conditions = nil) - Arel(table_name).where(construct_conditions(conditions, scope(:find))).delete + arel_table.where(construct_conditions(conditions, scope(:find))).delete end # Returns the result of an SQL statement that should only include a COUNT(*) in the SELECT part. @@ -2647,8 +2647,7 @@ module ActiveRecord #:nodoc: # be made (since they can't be persisted). def destroy unless new_record? - table = Arel(self.class.table_name) - table.where(table[self.class.primary_key].eq(quoted_id)).delete + arel_table.where(arel_table[self.class.primary_key].eq(id)).delete end freeze @@ -2943,7 +2942,7 @@ module ActiveRecord #:nodoc: def update(attribute_names = @attributes.keys) attributes_with_values = arel_attributes_values(false, false, attribute_names) return 0 if attributes_with_values.empty? - table.where(table[self.class.primary_key].eq(id)).update(attributes_with_values) + arel_table.where(arel_table[self.class.primary_key].eq(id)).update(attributes_with_values) end # Creates a record with values matching those of the instance attributes @@ -3053,7 +3052,7 @@ module ActiveRecord #:nodoc: include_readonly_attributes ? quoted : remove_readonly_attributes(quoted) end - def table + def arel_table @arel_table ||= Arel(self.class.table_name) end @@ -3065,7 +3064,7 @@ module ActiveRecord #:nodoc: value = read_attribute(name) if include_readonly_attributes || (!include_readonly_attributes && !self.class.readonly_attributes.include?(name)) - attrs[table[name]] = value + attrs[arel_table[name]] = value end end end diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index 9dbfcdf175..6bd2163dc2 100644 --- a/activerecord/lib/active_record/calculations.rb +++ b/activerecord/lib/active_record/calculations.rb @@ -124,6 +124,7 @@ module ActiveRecord # Person.sum("2 * age") def calculate(operation, column_name, options = {}) validate_calculation_options(operation, options) + operation = operation.to_s.downcase scope = scope(:find) @@ -135,7 +136,7 @@ module ActiveRecord joins << join_dependency.join_associations.collect{|join| join.association_join }.join end - if operation == :count + if operation == "count" if merged_includes.any? distinct = true column_name = options[:select] || primary_key @@ -162,7 +163,7 @@ module ActiveRecord def execute_simple_calculation(operation, column_name, options) #:nodoc: table = options[:from] || table_name - value = if operation == :count + value = if operation == 'count' if column_name == :all && options[:select].blank? column_name = "*" elsif !options[:select].blank? @@ -187,17 +188,15 @@ module ActiveRecord options[:group] = connection.adapter_name == 'FrontBase' ? group_alias : group_field aggregate_alias = column_alias_for(operation, column_name) - if operation == :count && column_name == :all + + if operation == 'count' && column_name == :all options[:select] = "COUNT(*) AS count_all, #{group_field} AS #{group_alias}" else - arel_column = Arel::Attribute.new(Arel(table_name), column_name).send(operation) + arel_column = Arel::Attribute.new(arel_table, column_name).send(operation) options[:select] = "#{arel_column.as(aggregate_alias).to_sql}, #{group_field} AS #{group_alias}" end - - sql = construct_finder_arel(options) - - calculated_data = connection.select_all(sql.to_sql) + calculated_data = connection.select_all(construct_finder_sql(options)) if association key_ids = calculated_data.collect { |row| row[group_alias] } @@ -275,7 +274,6 @@ module ActiveRecord end def type_cast_calculated_value(value, column, operation = nil) - operation = operation.to_s.downcase case operation when 'count' then value.to_i when 'sum' then type_cast_using_column(value || '0', column) diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb index f66458cc8c..a4f5dd8516 100644 --- a/activerecord/lib/active_record/locking/optimistic.rb +++ b/activerecord/lib/active_record/locking/optimistic.rb @@ -89,9 +89,9 @@ module ActiveRecord attribute_names.uniq! begin - affected_rows = table.where( - table[self.class.primary_key].eq(quoted_id).and( - table[self.class.locking_column].eq(quote_value(previous_value)) + affected_rows = arel_table.where( + arel_table[self.class.primary_key].eq(quoted_id).and( + arel_table[self.class.locking_column].eq(quote_value(previous_value)) ) ).update(arel_attributes_values(false, false, attribute_names)) @@ -116,9 +116,9 @@ module ActiveRecord lock_col = self.class.locking_column previous_value = send(lock_col).to_i - affected_rows = table.where( - table[self.class.primary_key].eq(quoted_id).and( - table[self.class.locking_column].eq(quote_value(previous_value)) + affected_rows = arel_table.where( + arel_table[self.class.primary_key].eq(quoted_id).and( + arel_table[self.class.locking_column].eq(quote_value(previous_value)) ) ).delete