Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

just use a hash for doing association caching

  • Loading branch information...
commit 839f3bf6822ed3698df1e606c4215d650312f33e 1 parent 8a1c533
Aaron Patterson tenderlove authored
14 activerecord/lib/active_record/associations.rb
View
@@ -132,24 +132,24 @@ module Associations # :nodoc:
# 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
4 activerecord/lib/active_record/associations/class_methods/join_dependency.rb
View
@@ -176,7 +176,7 @@ def construct(parent, associations, join_parts, row)
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 @@ def construct_association(record, join_part, row)
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
7 activerecord/lib/active_record/base.rb
View
@@ -1368,6 +1368,7 @@ def encode_quoted_value(value) #:nodoc:
# 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 @@ def encode_with(coder)
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 @@ def initialize_dup(other)
end
clear_aggregation_cache
- clear_association_cache
- @attributes_cache = {}
+
+ @association_cache = {}
+ @attributes_cache = {}
@new_record = true
ensure_proper_type
4 activerecord/test/cases/associations/belongs_to_associations_test.rb
View
@@ -78,14 +78,14 @@ def test_eager_loading_with_primary_key
Firm.create("name" => "Apple")
Client.create("name" => "Citibank", :firm_name => "Apple")
citibank_result = Client.find(:first, :conditions => {:name => "Citibank"}, :include => :firm_with_primary_key)
- assert_not_nil citibank_result.instance_variable_get("@firm_with_primary_key")
+ assert citibank_result.association_cache.key?(:firm_with_primary_key)
end
def test_eager_loading_with_primary_key_as_symbol
Firm.create("name" => "Apple")
Client.create("name" => "Citibank", :firm_name => "Apple")
citibank_result = Client.find(:first, :conditions => {:name => "Citibank"}, :include => :firm_with_primary_key_symbols)
- assert_not_nil citibank_result.instance_variable_get("@firm_with_primary_key_symbols")
+ assert citibank_result.association_cache.key?(:firm_with_primary_key_symbols)
end
def test_creating_the_belonging_object
2  activerecord/test/cases/inheritance_test.rb
View
@@ -203,7 +203,7 @@ def test_alt_complex_inheritance
def test_eager_load_belongs_to_something_inherited
account = Account.find(1, :include => :firm)
- assert_not_nil account.instance_variable_get("@firm"), "nil proves eager load failed"
+ assert account.association_cache.key?(:firm), "nil proves eager load failed"
end
def test_eager_load_belongs_to_primary_key_quoting
1  activerecord/test/cases/modules_test.rb
View
@@ -49,7 +49,6 @@ def test_associations_spanning_cross_modules
def test_find_account_and_include_company
account = MyApplication::Billing::Account.find(1, :include => :firm)
- assert_kind_of MyApplication::Business::Firm, account.instance_variable_get('@firm')
assert_kind_of MyApplication::Business::Firm, account.firm
end
Please sign in to comment.
Something went wrong with that request. Please try again.