Access record from ActiveRecord::RecordInvalid
You can pass an array of hashes to Thing.create!
in ActiveRecord. If one of those records is invalid, then an ActiveRecord::RecordInvalid error is thrown. You might need to know which record threw the error, in which case you can get the record from the error with record_invalid_error.record
bad_record = nil
begin
Things.create!([{value: 'bad'}, {value: 'good'}])
rescue ActiveRecord::RecordInvalid => record_invalid_error
bad_record = record_invalid_error.record
end
if bad_record
puts "got a bad record with value: #{bad_record.value}"
end
Tweet