Skip to content

Commit 2cdd229

Browse files
committed
Merge pull request rails#17808 from yuki24/fix-bug-where-record-not-saved-loses-error-message
Fixed a bug where AR::RecordNotSaved loses the given error message
2 parents 99e4ebb + 5142d54 commit 2cdd229

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

Diff for: activerecord/lib/active_record/errors.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ class RecordNotFound < ActiveRecordError
5454
class RecordNotSaved < ActiveRecordError
5555
attr_reader :record
5656

57-
def initialize(record)
57+
def initialize(message, record = nil)
5858
@record = record
59-
super()
59+
super(message)
6060
end
6161
end
6262

Diff for: activerecord/lib/active_record/persistence.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def save(*)
139139
# Attributes marked as readonly are silently ignored if the record is
140140
# being updated.
141141
def save!(*)
142-
create_or_update || raise(RecordNotSaved, self)
142+
create_or_update || raise(RecordNotSaved.new(nil, self))
143143
end
144144

145145
# Deletes the record in the database and freezes this instance to

Diff for: activerecord/test/cases/associations/has_many_associations_test.rb

+13-4
Original file line numberDiff line numberDiff line change
@@ -589,17 +589,21 @@ def test_adding_using_create
589589
end
590590

591591
def test_create_with_bang_on_has_many_when_parent_is_new_raises
592-
assert_raise(ActiveRecord::RecordNotSaved) do
592+
error = assert_raise(ActiveRecord::RecordNotSaved) do
593593
firm = Firm.new
594594
firm.plain_clients.create! :name=>"Whoever"
595595
end
596+
597+
assert_equal "You cannot call create unless the parent is saved", error.message
596598
end
597599

598600
def test_regular_create_on_has_many_when_parent_is_new_raises
599-
assert_raise(ActiveRecord::RecordNotSaved) do
601+
error = assert_raise(ActiveRecord::RecordNotSaved) do
600602
firm = Firm.new
601603
firm.plain_clients.create :name=>"Whoever"
602604
end
605+
606+
assert_equal "You cannot call create unless the parent is saved", error.message
603607
end
604608

605609
def test_create_with_bang_on_has_many_raises_when_record_not_saved
@@ -610,9 +614,11 @@ def test_create_with_bang_on_has_many_raises_when_record_not_saved
610614
end
611615

612616
def test_create_with_bang_on_habtm_when_parent_is_new_raises
613-
assert_raise(ActiveRecord::RecordNotSaved) do
617+
error = assert_raise(ActiveRecord::RecordNotSaved) do
614618
Developer.new("name" => "Aredridel").projects.create!
615619
end
620+
621+
assert_equal "You cannot call create unless the parent is saved", error.message
616622
end
617623

618624
def test_adding_a_mismatch_class
@@ -1353,10 +1359,13 @@ def test_replace_failure
13531359

13541360
assert !account.valid?
13551361
assert !orig_accounts.empty?
1356-
assert_raise ActiveRecord::RecordNotSaved do
1362+
error = assert_raise ActiveRecord::RecordNotSaved do
13571363
firm.accounts = [account]
13581364
end
1365+
13591366
assert_equal orig_accounts, firm.accounts
1367+
assert_equal "Failed to replace accounts because one or more of the " \
1368+
"new records could not be saved.", error.message
13601369
end
13611370

13621371
def test_replace_with_same_content

Diff for: activerecord/test/cases/associations/has_many_through_associations_test.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -615,8 +615,11 @@ def test_associate_with_create_exclamation_and_no_options
615615
def test_create_on_new_record
616616
p = Post.new
617617

618-
assert_raises(ActiveRecord::RecordNotSaved) { p.people.create(:first_name => "mew") }
619-
assert_raises(ActiveRecord::RecordNotSaved) { p.people.create!(:first_name => "snow") }
618+
error = assert_raises(ActiveRecord::RecordNotSaved) { p.people.create(:first_name => "mew") }
619+
assert_equal "You cannot call create unless the parent is saved", error.message
620+
621+
error = assert_raises(ActiveRecord::RecordNotSaved) { p.people.create!(:first_name => "snow") }
622+
assert_equal "You cannot call create unless the parent is saved", error.message
620623
end
621624

622625
def test_associate_with_create_and_invalid_options

Diff for: activerecord/test/cases/associations/has_one_associations_test.rb

+10-3
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,11 @@ def test_creation_failure_due_to_new_record_should_raise_error
410410
pirate = pirates(:redbeard)
411411
new_ship = Ship.new
412412

413-
assert_raise(ActiveRecord::RecordNotSaved) do
413+
error = assert_raise(ActiveRecord::RecordNotSaved) do
414414
pirate.ship = new_ship
415415
end
416+
417+
assert_equal "Failed to save the new associated ship.", error.message
416418
assert_nil pirate.ship
417419
assert_nil new_ship.pirate_id
418420
end
@@ -422,20 +424,25 @@ def test_replacement_failure_due_to_existing_record_should_raise_error
422424
pirate.ship.name = nil
423425

424426
assert !pirate.ship.valid?
425-
assert_raise(ActiveRecord::RecordNotSaved) do
427+
error = assert_raise(ActiveRecord::RecordNotSaved) do
426428
pirate.ship = ships(:interceptor)
427429
end
430+
428431
assert_equal ships(:black_pearl), pirate.ship
429432
assert_equal pirate.id, pirate.ship.pirate_id
433+
assert_equal "Failed to remove the existing associated ship. " +
434+
"The record failed to save after its foreign key was set to nil.", error.message
430435
end
431436

432437
def test_replacement_failure_due_to_new_record_should_raise_error
433438
pirate = pirates(:blackbeard)
434439
new_ship = Ship.new
435440

436-
assert_raise(ActiveRecord::RecordNotSaved) do
441+
error = assert_raise(ActiveRecord::RecordNotSaved) do
437442
pirate.ship = new_ship
438443
end
444+
445+
assert_equal "Failed to save the new associated ship.", error.message
439446
assert_equal ships(:black_pearl), pirate.ship
440447
assert_equal pirate.id, pirate.ship.pirate_id
441448
assert_equal pirate.id, ships(:black_pearl).reload.pirate_id

0 commit comments

Comments
 (0)