开发者

getting ActiveRecord::RecordNotSaved error while saving

开发者 https://www.devze.com 2023-01-22 08:50 出处:网络
While creating 开发者_如何学Pythona new object i am getting ActiveRecord::RecordNotSaved error on before_save.

While creating 开发者_如何学Pythona new object i am getting ActiveRecord::RecordNotSaved error on before_save.

But i want to fetch the proper message other than ActiveRecord::RecordNotSaved error message.

How may i fetch the proper error message and pass it to the rescue?

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


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


Why raise the exception and not just check if save or not ?

unless object.save
  object.errors
end
0

精彩评论

暂无评论...
验证码 换一张
取 消