aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb21
-rw-r--r--activerecord/lib/active_record/associations/association_proxy.rb29
-rw-r--r--activerecord/lib/active_record/associations/belongs_to_association.rb6
3 files changed, 33 insertions, 23 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index 334cdccc3d..f4d8be8b0f 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -90,27 +90,6 @@ module ActiveRecord
@loaded = true
end
- protected
- def quoted_record_ids(records)
- records.map { |record| record.quoted_id }.join(',')
- end
-
- def interpolate_sql_options!(options, *keys)
- keys.each { |key| options[key] &&= interpolate_sql(options[key]) }
- end
-
- def interpolate_sql(sql, record = nil)
- @owner.send(:interpolate_sql, sql, record)
- end
-
- def sanitize_sql(sql)
- @association_class.send(:sanitize_sql, sql)
- end
-
- def extract_options_from_args!(args)
- @owner.send(:extract_options_from_args!, args)
- end
-
private
def raise_on_type_mismatch(record)
raise ActiveRecord::AssociationTypeMismatch, "#{@association_class} expected, got #{record.class}" unless record.is_a?(@association_class)
diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb
index dcba207e20..424d7f818a 100644
--- a/activerecord/lib/active_record/associations/association_proxy.rb
+++ b/activerecord/lib/active_record/associations/association_proxy.rb
@@ -28,9 +28,30 @@ module ActiveRecord
@loaded
end
+ protected
+ def quoted_record_ids(records)
+ records.map { |record| record.quoted_id }.join(',')
+ end
+
+ def interpolate_sql_options!(options, *keys)
+ keys.each { |key| options[key] &&= interpolate_sql(options[key]) }
+ end
+
+ def interpolate_sql(sql, record = nil)
+ @owner.send(:interpolate_sql, sql, record)
+ end
+
+ def sanitize_sql(sql)
+ @association_class.send(:sanitize_sql, sql)
+ end
+
+ def extract_options_from_args!(args)
+ @owner.send(:extract_options_from_args!, args)
+ end
+
private
def load_target
- unless @owner.new_record?
+ if !@owner.new_record? || foreign_key_present
begin
@target = find_target if not loaded?
rescue ActiveRecord::RecordNotFound
@@ -41,6 +62,12 @@ module ActiveRecord
@target
end
+ # Can be overwritten by associations that might have the foreign key available for an association without
+ # having the object itself (and still being a new record). Currently, only belongs_to present this scenario.
+ def foreign_key_present
+ false
+ end
+
def raise_on_type_mismatch(record)
raise ActiveRecord::AssociationTypeMismatch, "#{@association_class} expected, got #{record.class}" unless record.is_a?(@association_class)
end
diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb
index aa627f7495..8de48cd880 100644
--- a/activerecord/lib/active_record/associations/belongs_to_association.rb
+++ b/activerecord/lib/active_record/associations/belongs_to_association.rb
@@ -45,12 +45,16 @@ module ActiveRecord
private
def find_target
if @options[:conditions]
- @association_class.find_on_conditions(@owner[@association_class_primary_key_name], @options[:conditions])
+ @association_class.find_on_conditions(@owner[@association_class_primary_key_name], interpolate_sql(@options[:conditions]))
else
@association_class.find(@owner[@association_class_primary_key_name])
end
end
+ def foreign_key_present
+ !@owner[@association_class_primary_key_name].nil?
+ end
+
def target_obsolete?
@owner[@association_class_primary_key_name] != @target.id
end