问题描述:

While creating a 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
  raise unless @object.save!
rescue ActiveRecord::RecordNotSaved => e
  @object.errors.full_messages
end

Updated

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
相关阅读:
Top