aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-01-17 00:30:53 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-01-17 00:30:53 +0000
commit1ae630a40473514254cf14162eae37b7d2330f70 (patch)
treed0a28fac97d53c8f47869661ec3e1e4e92e62de1 /activerecord/lib
parent3b9be80a3a6b83156562d218e459822a51d6795c (diff)
downloadrails-1ae630a40473514254cf14162eae37b7d2330f70.tar.gz
rails-1ae630a40473514254cf14162eae37b7d2330f70.tar.bz2
rails-1ae630a40473514254cf14162eae37b7d2330f70.zip
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
Diffstat (limited to 'activerecord/lib')
-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