ActiveRecord Bang methods
suggest changeIf you need an Active Record method to raise an exception instead of returning false or nil on failure, add ! to the method name. Some failures are easy to miss without it. During development, prefer these bang methods so invalid data raises immediately.
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
Active Record methods that accept a bang (!) include:
.create!.take!.first!.last!.find_by!.find_or_create_by!#save!#update!- dynamic finders such as
find_by_name!
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents