Ticket #8117: calculate_grouped_association_with_foreign_key.patch
| File calculate_grouped_association_with_foreign_key.patch, 1.7 kB (added by kamal, 1 year ago) |
|---|
-
activerecord/test/calculations_test.rb
old new 159 159 assert_equal 1, c.first.last 160 160 end 161 161 end 162 162 163 def test_should_calculate_grouped_association_with_foreign_key_option 164 Account.belongs_to :another_firm, :class_name => 'Firm', :foreign_key => 'firm_id' 165 c = Account.count(:all, :group => :another_firm) 166 assert_equal 1, c[companies(:first_firm)] 167 assert_equal 2, c[companies(:rails_core)] 168 assert_equal 1, c[companies(:first_client)] 169 end 170 163 171 def test_should_not_modify_options_when_using_includes 164 172 options = {:conditions => 'companies.id > 1', :include => :firm} 165 173 options_copy = options.dup -
activerecord/lib/active_record/calculations.rb
old new 221 221 group_attr = options[:group].to_s 222 222 association = reflect_on_association(group_attr.to_sym) 223 223 associated = association && association.macro == :belongs_to # only count belongs_to associations 224 group_field = (associated ? "#{options[:group]}_id" : options[:group]).to_s224 group_field = associated ? association.primary_key_name : group_attr 225 225 group_alias = column_alias_for(group_field) 226 226 group_column = column_for group_field 227 227 sql = construct_calculation_sql(operation, column_name, options.merge(:group_field => group_field, :group_alias => group_alias))