aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/aggregations.rb6
-rwxr-xr-xactiverecord/lib/active_record/base.rb1
-rw-r--r--activerecord/test/aggregations_test.rb19
-rw-r--r--activerecord/test/fixtures/customer.rb4
5 files changed, 32 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index e4ab429e6a..359e14619f 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Reloading an instance refreshes its aggregations as well as its associations. #3024 [François Beausolei]
+
* Fixed that using :include together with :conditions array in Base.find would cause NoMethodError #2887 [Paul Hammmond]
* PostgreSQL: more robust sequence name discovery. #3087 [Rick Olson]
diff --git a/activerecord/lib/active_record/aggregations.rb b/activerecord/lib/active_record/aggregations.rb
index 0970eaceee..314c40cfde 100644
--- a/activerecord/lib/active_record/aggregations.rb
+++ b/activerecord/lib/active_record/aggregations.rb
@@ -4,6 +4,12 @@ module ActiveRecord
base.extend(ClassMethods)
end
+ def clear_aggregation_cache #:nodoc:
+ self.class.reflect_on_all_aggregations.to_a.each do |assoc|
+ instance_variable_set "@#{assoc.name}", nil
+ end unless self.new_record?
+ end
+
# Active Record implements aggregation through a macro-like class method called +composed_of+ for representing attributes
# as value objects. It expresses relationships like "Account [is] composed of Money [among other things]" or "Person [is]
# composed of [an] address". Each call to the macro adds a description of how the value objects are created from the
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 27b2de6f8d..b8925a6ee5 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1309,6 +1309,7 @@ module ActiveRecord #:nodoc:
# Reloads the attributes of this object from the database.
def reload
+ clear_aggregation_cache
clear_association_cache
@attributes.update(self.class.find(self.id).instance_variable_get('@attributes'))
self
diff --git a/activerecord/test/aggregations_test.rb b/activerecord/test/aggregations_test.rb
index 8be0b20198..39adedae53 100644
--- a/activerecord/test/aggregations_test.rb
+++ b/activerecord/test/aggregations_test.rb
@@ -44,4 +44,23 @@ class AggregationsTest < Test::Unit::TestCase
assert_equal "39", customers(:david).gps_location.latitude
assert_equal "-110", customers(:david).gps_location.longitude
end
+
+ def test_reloaded_instance_refreshes_aggregations
+ assert_equal "35.544623640962634", customers(:david).gps_location.latitude
+ assert_equal "-105.9309951055148", customers(:david).gps_location.longitude
+
+ Customer.update_all("gps_location = '24x113'")
+ customers(:david).reload
+ assert_equal '24x113', customers(:david)['gps_location']
+
+ assert_equal GpsLocation.new('24x113'), customers(:david).gps_location
+ end
+
+ def test_gps_equality
+ assert GpsLocation.new('39x110') == GpsLocation.new('39x110')
+ end
+
+ def test_gps_inequality
+ assert GpsLocation.new('39x110') != GpsLocation.new('39x111')
+ end
end
diff --git a/activerecord/test/fixtures/customer.rb b/activerecord/test/fixtures/customer.rb
index c36d4d33a8..2eba052eee 100644
--- a/activerecord/test/fixtures/customer.rb
+++ b/activerecord/test/fixtures/customer.rb
@@ -44,4 +44,8 @@ class GpsLocation
def longitude
gps_location.split("x").last
end
+
+ def ==(other)
+ self.latitude == other.latitude && self.longitude == other.longitude
+ end
end \ No newline at end of file