保存时收到ActiveRecord :: RecordNotSaved错误

时间:2010-10-29 16:53:39

标签: ruby-on-rails ruby

创建新对象时,我在before_save上收到ActiveRecord :: RecordNotSaved错误。

但我想获取除ActiveRecord :: RecordNotSaved错误消息之外的正确消息。

我如何获取正确的错误消息并将其传递给救援人员?

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

2 个答案:

答案 0 :(得分:10)

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

答案 1 :(得分:6)

为什么要提出异常,而不仅仅是检查是否保存?

unless object.save
  object.errors
end