aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/association_proxy.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/associations/association_proxy.rb')
-rw-r--r--activerecord/lib/active_record/associations/association_proxy.rb31
1 files changed, 23 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb
index 97eab8a793..7e68241a2c 100644
--- a/activerecord/lib/active_record/associations/association_proxy.rb
+++ b/activerecord/lib/active_record/associations/association_proxy.rb
@@ -8,7 +8,7 @@ module ActiveRecord
#
# AssociationProxy
# BelongsToAssociation
- # BelongsToPolymorphicAssociation
+ # BelongsToPolymorphicAssociation
# AssociationCollection + HasAssociation
# HasAndBelongsToManyAssociation
# HasManyAssociation
@@ -116,6 +116,7 @@ module ActiveRecord
# Reloads the \target and returns +self+ on success.
def reload
reset
+ construct_scope
load_target
self unless @target.nil?
end
@@ -166,6 +167,10 @@ module ActiveRecord
end
end
+ def scoped
+ with_scope(@scope) { target_klass.scoped }
+ end
+
protected
def interpolate_sql(sql, record = nil)
@owner.send(:interpolate_sql, sql, record)
@@ -192,15 +197,19 @@ module ActiveRecord
# Forwards +with_scope+ to the reflection.
def with_scope(*args, &block)
- @reflection.klass.send :with_scope, *args, &block
+ target_klass.send :with_scope, *args, &block
end
# Construct the scope used for find/create queries on the target
def construct_scope
- @scope = {
- :find => construct_find_scope,
- :create => construct_create_scope
- }
+ if target_klass
+ @scope = {
+ :find => construct_find_scope,
+ :create => construct_create_scope
+ }
+ else
+ @scope = nil
+ end
end
# Implemented by subclasses
@@ -214,7 +223,7 @@ module ActiveRecord
end
def aliased_table
- @reflection.klass.arel_table
+ target_klass.arel_table
end
# Set the inverse association, if possible
@@ -224,6 +233,12 @@ module ActiveRecord
end
end
+ # This class of the target. belongs_to polymorphic overrides this to look at the
+ # polymorphic_type field on the owner.
+ def target_klass
+ @reflection.klass
+ end
+
private
# Forwards any missing method call to the \target.
def method_missing(method, *args)
@@ -254,7 +269,7 @@ module ActiveRecord
def load_target
return nil unless defined?(@loaded)
- if !loaded? && (!@owner.new_record? || foreign_key_present)
+ if !loaded? && (!@owner.new_record? || foreign_key_present) && @scope
@target = find_target
end