krunal shah
krunal shah

Reputation: 16339

getting ActiveRecord::RecordNotSaved error while saving

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

Upvotes: 3

Views: 15019

Answers (2)

krunal shah
krunal shah

Reputation: 16339

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

Upvotes: 12

shingara
shingara

Reputation: 46914

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

unless object.save
  object.errors
end

Upvotes: 6

Related Questions