diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-02-20 17:45:10 -0200 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-02-20 17:45:10 -0200 |
commit | 37bef5827f94137f4542cacc669e9c3ce6c9495d (patch) | |
tree | 3c0b9379da5e2f68a459e52ac0910e51ee0bf85c /activerecord/lib/active_record/associations.rb | |
parent | 192d319fdbb5b1575b5541bb5395c87d87fadfa1 (diff) | |
parent | 9d569585a20ddd9ddb3602921f2ccffc208998d8 (diff) | |
download | rails-37bef5827f94137f4542cacc669e9c3ce6c9495d.tar.gz rails-37bef5827f94137f4542cacc669e9c3ce6c9495d.tar.bz2 rails-37bef5827f94137f4542cacc669e9c3ce6c9495d.zip |
Merge pull request #16989 from Empact/reload-cache-clear
Isolate access to @associations_cache and @aggregations_cache to the Associations and Aggregations modules, respectively.
Diffstat (limited to 'activerecord/lib/active_record/associations.rb')
-rw-r--r-- | activerecord/lib/active_record/associations.rb | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index a146d78a5a..499b00a815 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -145,20 +145,14 @@ module ActiveRecord autoload :AliasTracker, 'active_record/associations/alias_tracker' end - # Clears out the association cache. - def clear_association_cache #:nodoc: - @association_cache.clear if persisted? - end - - # :nodoc: - attr_reader :association_cache - # Returns the association instance for the given name, instantiating it if it doesn't already exist def association(name) #:nodoc: association = association_instance_get(name) if association.nil? - raise AssociationNotFoundError.new(self, name) unless reflection = self.class._reflect_on_association(name) + unless reflection = self.class._reflect_on_association(name) + raise AssociationNotFoundError.new(self, name) + end association = reflection.association_class.new(self, reflection) association_instance_set(name, association) end @@ -166,7 +160,31 @@ module ActiveRecord association end + def association_cached?(name) # :nodoc + @association_cache.key?(name) + end + + def initialize_dup(*) # :nodoc: + @association_cache = {} + super + end + + def reload(*) # :nodoc: + clear_association_cache + super + end + private + # Clears out the association cache. + def clear_association_cache # :nodoc: + @association_cache.clear if persisted? + end + + def init_internals # :nodoc: + @association_cache = {} + super + end + # Returns the specified association instance if it responds to :loaded?, nil otherwise. def association_instance_get(name) @association_cache[name] |