aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/nested_attributes.rb
diff options
context:
space:
mode:
authorEloy Duran <eloy.de.enige@gmail.com>2010-01-03 22:48:25 +0100
committerEloy Duran <eloy.de.enige@gmail.com>2010-01-07 13:19:49 +0100
commit9550916903c931161f04a98091fba712d7fa5c1d (patch)
tree2eca315461c0f3aac694034a300bd9df0e134381 /activerecord/lib/active_record/nested_attributes.rb
parentb6264c43f414f323656ed135d46213466cbe00fb (diff)
downloadrails-9550916903c931161f04a98091fba712d7fa5c1d.tar.gz
rails-9550916903c931161f04a98091fba712d7fa5c1d.tar.bz2
rails-9550916903c931161f04a98091fba712d7fa5c1d.zip
Raise a RecordNotFound if an ID in nested attributes is given but doesn't return a record. [#2415 state:resolved]
Diffstat (limited to 'activerecord/lib/active_record/nested_attributes.rb')
-rw-r--r--activerecord/lib/active_record/nested_attributes.rb11
1 files changed, 11 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb
index e1e80355c0..9038888d22 100644
--- a/activerecord/lib/active_record/nested_attributes.rb
+++ b/activerecord/lib/active_record/nested_attributes.rb
@@ -288,6 +288,10 @@ module ActiveRecord
if check_existing_record && (record = send(association_name)) &&
(options[:update_only] || record.id.to_s == attributes['id'].to_s)
assign_to_or_mark_for_destruction(record, attributes, options[:allow_destroy])
+
+ elsif attributes['id']
+ raise_nested_attributes_record_not_found(association_name, attributes['id'])
+
elsif !reject_new_record?(association_name, attributes)
method = "build_#{association_name}"
if respond_to?(method)
@@ -349,6 +353,8 @@ module ActiveRecord
end
elsif existing_record = send(association_name).detect { |record| record.id.to_s == attributes['id'].to_s }
assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
+ else
+ raise_nested_attributes_record_not_found(association_name, attributes['id'])
end
end
end
@@ -383,5 +389,10 @@ module ActiveRecord
callback.call(attributes)
end
end
+
+ def raise_nested_attributes_record_not_found(association_name, record_id)
+ reflection = self.class.reflect_on_association(association_name)
+ raise RecordNotFound, "Couldn't find #{reflection.klass.name} with ID=#{record_id} for #{self.class.name} with ID=#{id}"
+ end
end
end