aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-01-24 20:47:06 +0000
committerJon Leighton <j@jonathanleighton.com>2011-01-30 11:56:41 +0000
commitde05e2fb15ee4fd521aae202eb4517ae05114c28 (patch)
treea59226ca147aec24ebcbbd10dd01f02e902ff108
parentd392c67d2c614644d678627e6cd0124878982fc7 (diff)
downloadrails-de05e2fb15ee4fd521aae202eb4517ae05114c28.tar.gz
rails-de05e2fb15ee4fd521aae202eb4517ae05114c28.tar.bz2
rails-de05e2fb15ee4fd521aae202eb4517ae05114c28.zip
Abstract load_target conditional logic
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb2
-rw-r--r--activerecord/lib/active_record/associations/association_proxy.rb11
2 files changed, 7 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index 3c939b0ef0..95526be82f 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -359,7 +359,7 @@ module ActiveRecord
end
def load_target
- if (!@owner.new_record? || foreign_key_present?) && !loaded?
+ if find_target?
targets = []
begin
diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb
index 59b0c54f2f..ead2c5ede2 100644
--- a/activerecord/lib/active_record/associations/association_proxy.rb
+++ b/activerecord/lib/active_record/associations/association_proxy.rb
@@ -226,18 +226,19 @@ module ActiveRecord
# ActiveRecord::RecordNotFound is rescued within the method, and it is
# not reraised. The proxy is \reset and +nil+ is the return value.
def load_target
- if !loaded? && (!@owner.new_record? || foreign_key_present?) && target_klass
- @target = find_target
- end
-
+ @target = find_target if find_target?
loaded
- @target
+ target
rescue ActiveRecord::RecordNotFound
reset
end
private
+ def find_target?
+ !loaded? && (!@owner.new_record? || foreign_key_present?) && target_klass
+ end
+
def interpolate_sql(sql, record = nil)
@owner.send(:interpolate_sql, sql, record)
end