diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-01-07 11:28:11 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-01-07 11:28:11 -0800 |
commit | 839f3bf6822ed3698df1e606c4215d650312f33e (patch) | |
tree | 1f53277c77c8b64679f9bfda1e184a0c4ce91730 /activerecord/lib | |
parent | 8a1c5337808c53c5e9d2d842a09a90599c497e89 (diff) | |
download | rails-839f3bf6822ed3698df1e606c4215d650312f33e.tar.gz rails-839f3bf6822ed3698df1e606c4215d650312f33e.tar.bz2 rails-839f3bf6822ed3698df1e606c4215d650312f33e.zip |
just use a hash for doing association caching
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations.rb | 14 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/class_methods/join_dependency.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/base.rb | 7 |
3 files changed, 14 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index f6a7fd3ca0..78652ba0c5 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -132,24 +132,24 @@ module ActiveRecord # Clears out the association cache. def clear_association_cache #:nodoc: - self.class.reflect_on_all_associations.to_a.each do |assoc| - instance_variable_set "@#{assoc.name}", nil - end if persisted? + @association_cache.clear if persisted? end + # :nodoc: + attr_reader :association_cache + private # Returns the specified association instance if it responds to :loaded?, nil otherwise. def association_instance_get(name) - ivar = "@#{name}" - if instance_variable_defined?(ivar) - association = instance_variable_get(ivar) + if @association_cache.key? name + association = @association_cache[name] association if association.respond_to?(:loaded?) end end # Set the specified association instance. def association_instance_set(name, association) - instance_variable_set("@#{name}", association) + @association_cache[name] = association end # Associations are a set of macro-like class methods for tying objects together through diff --git a/activerecord/lib/active_record/associations/class_methods/join_dependency.rb b/activerecord/lib/active_record/associations/class_methods/join_dependency.rb index bb6145656e..6263c4e3b0 100644 --- a/activerecord/lib/active_record/associations/class_methods/join_dependency.rb +++ b/activerecord/lib/active_record/associations/class_methods/join_dependency.rb @@ -176,7 +176,7 @@ module ActiveRecord join_part = join_parts.detect { |j| j.reflection.name.to_s == name && - j.parent_table_name == parent.class.table_name } + j.parent_table_name == parent.class.table_name } raise(ConfigurationError, "No such association") unless join_part @@ -201,7 +201,7 @@ module ActiveRecord macro = join_part.reflection.macro if macro == :has_one - return if record.instance_variable_defined?("@#{join_part.reflection.name}") + return if record.association_cache.key?(join_part.reflection.name) association = join_part.instantiate(row) unless row[join_part.aliased_primary_key].nil? set_target_and_inverse(join_part, association, record) else diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index d3a739b98b..f32132b18a 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1368,6 +1368,7 @@ MSG # hence you can't have attributes that aren't part of the table columns. def initialize(attributes = nil) @attributes = attributes_from_column_definition + @association_cache = {} @attributes_cache = {} @new_record = true @readonly = false @@ -1415,6 +1416,7 @@ MSG def init_with(coder) @attributes = coder['attributes'] @attributes_cache, @previously_changed, @changed_attributes = {}, {}, {} + @association_cache = {} @readonly = @destroyed = @marked_for_destruction = false @new_record = false _run_find_callbacks @@ -1627,8 +1629,9 @@ MSG end clear_aggregation_cache - clear_association_cache - @attributes_cache = {} + + @association_cache = {} + @attributes_cache = {} @new_record = true ensure_proper_type |