From 1ae630a40473514254cf14162eae37b7d2330f70 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 17 Jan 2005 00:30:53 +0000 Subject: belongs_to association should always honor a present foreign key and condition interpolation should also be possible on belongs_to git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@433 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../associations/association_collection.rb | 21 ---------------- .../associations/association_proxy.rb | 29 +++++++++++++++++++++- .../associations/belongs_to_association.rb | 6 ++++- 3 files changed, 33 insertions(+), 23 deletions(-) (limited to 'activerecord/lib/active_record') 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 -- cgit v1.2.3