Permalink
Browse files

Fixed bug in active_record/nested_attributes where an empty string id…

… caused an ActiveRecord::RecordNotFound error. Found by [Ben Tillman]

[#5638 state:committed]

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
  • Loading branch information...
1 parent ceef719 commit 894fd288596b005148c3882e6431f6453d198101 @spastorino spastorino committed Nov 11, 2010
@@ -323,7 +323,7 @@ def assign_nested_attributes_for_one_to_one_association(association_name, attrib
(options[:update_only] || record.id.to_s == attributes['id'].to_s)
assign_to_or_mark_for_destruction(record, attributes, options[:allow_destroy]) unless call_reject_if(association_name, attributes)
- elsif attributes['id']
+ elsif not attributes['id'].blank?
raise_nested_attributes_record_not_found(association_name, attributes['id'])
elsif !reject_new_record?(association_name, attributes)
@@ -139,6 +139,14 @@ def test_has_many_association_updating_a_single_record
assert_equal 'gardening', interest.reload.topic
end
+ def test_reject_if_with_blank_nested_attributes_id
+ # When using a select list to choose an existing 'ship' id, with :include_blank => true
+ Pirate.accepts_nested_attributes_for :ship, :reject_if => proc {|attributes| attributes[:id].blank? }
+
+ pirate = Pirate.new(:catchphrase => "Stop wastin' me time")
+ pirate.ship_attributes = { :id => "" }
+ assert_nothing_raised(ActiveRecord::RecordNotFound) { pirate.save! }
+ end
end
class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase

0 comments on commit 894fd28

Please sign in to comment.