If you need an ActiveRecord method to raise an exception instead of a false
value in case of failure, you can add !
to them. This is very important. As some exceptions/failures are hard to catch if you don't use ! on them. I recommended doing this in your development cycle to write all your ActiveRecord code this way to save you time and trouble.
Class User < ActiveRecord::Base
validates :last_name, presence: true
end
User.create!(first_name: "John")
#=> ActiveRecord::RecordInvalid: Validation failed: Last name can't be blank
The ActiveRecord methods which accept a bang (!
) are:
.create!
.take!
.first!
.last!
.find_by!
.find_or_create_by!
#save!
#update!