Permalink
Browse files

Fix calculations on associations with custom :foreign_key. Closes #8117

… [kamal, jack]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8778 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 6d8534a commit 5bbc461fcbe18b0ab69c459a94cf0b58f5a4e6b9 @jeremy jeremy committed Feb 2, 2008
@@ -213,7 +213,7 @@ def execute_grouped_calculation(operation, column_name, column, options) #:nodoc
group_attr = options[:group].to_s
association = reflect_on_association(group_attr.to_sym)
associated = association && association.macro == :belongs_to # only count belongs_to associations
- group_field = (associated ? "#{options[:group]}_id" : options[:group]).to_s
+ group_field = associated ? association.primary_key_name : group_attr
group_alias = column_alias_for(group_field)
group_column = column_for group_field
sql = construct_calculation_sql(operation, column_name, options.merge(:group_field => group_field, :group_alias => group_alias))
@@ -160,6 +160,14 @@ def test_should_group_by_association_with_non_numeric_foreign_key
end
end
+ def test_should_calculate_grouped_association_with_foreign_key_option
+ Account.belongs_to :another_firm, :class_name => 'Firm', :foreign_key => 'firm_id'
+ c = Account.count(:all, :group => :another_firm)
+ assert_equal 1, c[companies(:first_firm)]
+ assert_equal 2, c[companies(:rails_core)]
+ assert_equal 1, c[companies(:first_client)]
+ end
+
def test_should_not_modify_options_when_using_includes
options = {:conditions => 'companies.id > 1', :include => :firm}
options_copy = options.dup
@@ -0,0 +1,24 @@
+require 'cases/helper'
+require 'models/topic'
+
+class InvalidDateTest < Test::Unit::TestCase
+ def test_assign_valid_dates
+ valid_dates = [[2007, 11, 30], [1993, 2, 28], [2008, 2, 29]]
+
+ invalid_dates = [[2007, 11, 31], [1993, 2, 29], [2007, 2, 29]]
+
+ topic = Topic.new
+
+ valid_dates.each do |date_src|
+ topic = Topic.new("last_read(1i)" => date_src[0].to_s, "last_read(2i)" => date_src[1].to_s, "last_read(3i)" => date_src[2].to_s)
+ assert_equal(topic.last_read, Date.new(*date_src))
+ end
+
+ invalid_dates.each do |date_src|
+ assert_nothing_raised do
+ topic = Topic.new({"last_read(1i)" => date_src[0].to_s, "last_read(2i)" => date_src[1].to_s, "last_read(3i)" => date_src[2].to_s})
+ assert_equal(topic.last_read, Time.local(*date_src).to_date, "The date should be modified according to the behaviour of the Time object")
+ end
+ end
+ end
+end

0 comments on commit 5bbc461

Please sign in to comment.