Ruby on Rails ActiveRecord Bang(!)方法
示例
如果您需要一个ActiveRecord方法来引发一个异常而不是一个false值,以防失败,则可以添加!到它们中。这个非常重要。如果您不使用某些异常/故障,将很难发现!在他们。我建议您在开发周期中执行此操作,以这种方式编写所有ActiveRecord代码,以节省时间和麻烦。
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
接受bang()的ActiveRecord方法是:!
.create!
.take!
.first!
.last!
.find_by!
.find_or_create_by!
#save!
#update!
所有AR动态查找器