Getting ActiveRecord :: RecordNotSaved when saving

When creating a new object, I get an ActiveRecord :: RecordNotSaved error on before_save.

But I want to get the correct message, other than the ActiveRecord :: RecordNotSaved error message.

How can I get the correct error message and pass it to help?

begin

  #some logic
  raise unless object.save!
rescue ActiveRecord::RecordNotSaved => e
  # How may fetch proper message where my object is failing here ..
  # like object.errors.message or something like that.
end
+3
source share
2 answers
begin
  #some logic
  raise unless @object.save!
rescue ActiveRecord::RecordNotSaved => e
  @object.errors.full_messages
end

Update

begin
  #some logic
  @object.save!
rescue ActiveRecord::RecordNotSaved => e
  @object.errors.full_messages
end
+10
source

Why raise an exception, and not just check if it is saved or not?

unless object.save
  object.errors
end
+6
source

Source: https://habr.com/ru/post/1772162/


All Articles