Skip to content

Commit

Permalink
Merge pull request #17824 from yuki24/change-record-not-saved-and-not…
Browse files Browse the repository at this point in the history
…-destroyed-to-include-error-msg

AR::RecordNotSaved & RecordNotDestroyed from save!/destroy! should include an error message
  • Loading branch information
rafaelfranca committed May 3, 2015
1 parent 8216699 commit c61b17d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/errors.rb
Expand Up @@ -71,9 +71,9 @@ def initialize(message, record = nil)
class RecordNotDestroyed < ActiveRecordError
attr_reader :record

def initialize(record)
def initialize(message, record = nil)
@record = record
super()
super(message)
end
end

Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/persistence.rb
Expand Up @@ -139,7 +139,7 @@ def save(*)
# Attributes marked as readonly are silently ignored if the record is
# being updated.
def save!(*)
create_or_update || raise(RecordNotSaved.new(nil, self))
create_or_update || raise(RecordNotSaved.new("Failed to save the record", self))
end

# Deletes the record in the database and freezes this instance to
Expand Down Expand Up @@ -182,7 +182,7 @@ def destroy
# and <tt>destroy!</tt> raises ActiveRecord::RecordNotDestroyed. See
# ActiveRecord::Callbacks for further details.
def destroy!
destroy || raise(ActiveRecord::RecordNotDestroyed, self)
destroy || raise(RecordNotDestroyed.new("Failed to destroy the record", self))
end

# Returns an instance of the specified +klass+ with the attributes of the
Expand Down
Expand Up @@ -2017,11 +2017,12 @@ def test_collection_association_with_private_kernel_method
car = Car.create!
original_child = FailedBulb.create!(car: car)

assert_raise(ActiveRecord::RecordNotDestroyed) do
error = assert_raise(ActiveRecord::RecordNotDestroyed) do
car.failed_bulbs = [FailedBulb.create!]
end

assert_equal [original_child], car.reload.failed_bulbs
assert_equal "Failed to destroy the record", error.message
end

test 'updates counter cache when default scope is given' do
Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/cases/callbacks_test.rb
Expand Up @@ -445,6 +445,8 @@ def test_before_save_returning_false
assert !david.save
exc = assert_raise(ActiveRecord::RecordNotSaved) { david.save! }
assert_equal exc.record, david
assert_equal "Failed to save the record", exc.message
assert_equal exc.record, david

david = ImmutableDeveloper.find(1)
david.salary = 10_000_000
Expand Down Expand Up @@ -480,6 +482,8 @@ def test_before_destroy_returning_false
assert !david.destroy
exc = assert_raise(ActiveRecord::RecordNotDestroyed) { david.destroy! }
assert_equal exc.record, david
assert_equal "Failed to destroy the record", exc.message
assert_equal exc.record, david
assert_not_nil ImmutableDeveloper.find_by_id(1)

someone = CallbackCancellationDeveloper.find(1)
Expand Down

0 comments on commit c61b17d

Please sign in to comment.