Skip to content

Commit 839f3bf

Browse files
committed
just use a hash for doing association caching
1 parent 8a1c533 commit 839f3bf

File tree

6 files changed

+17
-15
lines changed

6 files changed

+17
-15
lines changed

activerecord/lib/active_record/associations.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,24 +132,24 @@ module Associations # :nodoc:
132132

133133
# Clears out the association cache.
134134
def clear_association_cache #:nodoc:
135-
self.class.reflect_on_all_associations.to_a.each do |assoc|
136-
instance_variable_set "@#{assoc.name}", nil
137-
end if persisted?
135+
@association_cache.clear if persisted?
138136
end
139137

138+
# :nodoc:
139+
attr_reader :association_cache
140+
140141
private
141142
# Returns the specified association instance if it responds to :loaded?, nil otherwise.
142143
def association_instance_get(name)
143-
ivar = "@#{name}"
144-
if instance_variable_defined?(ivar)
145-
association = instance_variable_get(ivar)
144+
if @association_cache.key? name
145+
association = @association_cache[name]
146146
association if association.respond_to?(:loaded?)
147147
end
148148
end
149149

150150
# Set the specified association instance.
151151
def association_instance_set(name, association)
152-
instance_variable_set("@#{name}", association)
152+
@association_cache[name] = association
153153
end
154154

155155
# Associations are a set of macro-like class methods for tying objects together through

activerecord/lib/active_record/associations/class_methods/join_dependency.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def construct(parent, associations, join_parts, row)
176176

177177
join_part = join_parts.detect { |j|
178178
j.reflection.name.to_s == name &&
179-
j.parent_table_name == parent.class.table_name }
179+
j.parent_table_name == parent.class.table_name }
180180

181181
raise(ConfigurationError, "No such association") unless join_part
182182

@@ -201,7 +201,7 @@ def construct_association(record, join_part, row)
201201

202202
macro = join_part.reflection.macro
203203
if macro == :has_one
204-
return if record.instance_variable_defined?("@#{join_part.reflection.name}")
204+
return if record.association_cache.key?(join_part.reflection.name)
205205
association = join_part.instantiate(row) unless row[join_part.aliased_primary_key].nil?
206206
set_target_and_inverse(join_part, association, record)
207207
else

activerecord/lib/active_record/base.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,7 @@ def encode_quoted_value(value) #:nodoc:
13681368
# hence you can't have attributes that aren't part of the table columns.
13691369
def initialize(attributes = nil)
13701370
@attributes = attributes_from_column_definition
1371+
@association_cache = {}
13711372
@attributes_cache = {}
13721373
@new_record = true
13731374
@readonly = false
@@ -1415,6 +1416,7 @@ def encode_with(coder)
14151416
def init_with(coder)
14161417
@attributes = coder['attributes']
14171418
@attributes_cache, @previously_changed, @changed_attributes = {}, {}, {}
1419+
@association_cache = {}
14181420
@readonly = @destroyed = @marked_for_destruction = false
14191421
@new_record = false
14201422
_run_find_callbacks
@@ -1627,8 +1629,9 @@ def initialize_dup(other)
16271629
end
16281630

16291631
clear_aggregation_cache
1630-
clear_association_cache
1631-
@attributes_cache = {}
1632+
1633+
@association_cache = {}
1634+
@attributes_cache = {}
16321635
@new_record = true
16331636

16341637
ensure_proper_type

activerecord/test/cases/associations/belongs_to_associations_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ def test_eager_loading_with_primary_key
7878
Firm.create("name" => "Apple")
7979
Client.create("name" => "Citibank", :firm_name => "Apple")
8080
citibank_result = Client.find(:first, :conditions => {:name => "Citibank"}, :include => :firm_with_primary_key)
81-
assert_not_nil citibank_result.instance_variable_get("@firm_with_primary_key")
81+
assert citibank_result.association_cache.key?(:firm_with_primary_key)
8282
end
8383

8484
def test_eager_loading_with_primary_key_as_symbol
8585
Firm.create("name" => "Apple")
8686
Client.create("name" => "Citibank", :firm_name => "Apple")
8787
citibank_result = Client.find(:first, :conditions => {:name => "Citibank"}, :include => :firm_with_primary_key_symbols)
88-
assert_not_nil citibank_result.instance_variable_get("@firm_with_primary_key_symbols")
88+
assert citibank_result.association_cache.key?(:firm_with_primary_key_symbols)
8989
end
9090

9191
def test_creating_the_belonging_object

activerecord/test/cases/inheritance_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def test_alt_complex_inheritance
203203

204204
def test_eager_load_belongs_to_something_inherited
205205
account = Account.find(1, :include => :firm)
206-
assert_not_nil account.instance_variable_get("@firm"), "nil proves eager load failed"
206+
assert account.association_cache.key?(:firm), "nil proves eager load failed"
207207
end
208208

209209
def test_eager_load_belongs_to_primary_key_quoting

activerecord/test/cases/modules_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ def test_associations_spanning_cross_modules
4949

5050
def test_find_account_and_include_company
5151
account = MyApplication::Billing::Account.find(1, :include => :firm)
52-
assert_kind_of MyApplication::Business::Firm, account.instance_variable_get('@firm')
5352
assert_kind_of MyApplication::Business::Firm, account.firm
5453
end
5554

0 commit comments

Comments
 (0)