跳至内容 跳至搜索

Active Record RecordInvalid

当记录无效时,由 ActiveRecord::Base#save!ActiveRecord::Base#create! 抛出。使用 record 方法检索未通过验证的记录。

begin
  complex_operation_that_internally_calls_save!
rescue ActiveRecord::RecordInvalid => invalid
  puts invalid.record.errors
end
方法
N

属性

[R] record

类公共方法

new(record = nil)

# File activerecord/lib/active_record/validations.rb, line 18
def initialize(record = nil)
  if record
    @record = record
    errors = @record.errors.full_messages.join(", ")
    message = I18n.t(:"#{@record.class.i18n_scope}.errors.messages.record_invalid", errors: errors, default: :"errors.messages.record_invalid")
  else
    message = "Record invalid"
  end

  super(message)
end