当我尝试更新字段时出现错误Validation failed
,但不确定不确定validates
的工作原理
我有一个Booking
模型,定义如下:
class Booking < ApplicationRecord
include ActiveModel::Dirty
validates :requester_status,
inclusion: { in: %w[pending approved cancelled expired] }
validates :host_status,
inclusion: { in: %w[pending approved declined cancelled flexible_dates expired] }
validates :start_at, date: { after: Date.yesterday }, presence: true
validates :end_at, date: { after: :start_at }, presence: true
end
我定义了一个更新hosts_status
和requester_status
的函数,如下所示:
def self.set_expired
pending_bookings = Booking.where(host_status: 'pending').where('end_at < ?', Date.today)
pending_bookings.each do |pending_booking|
pending_booking.host_status = 'expired'
pending_booking.requester_status = 'expired'
pending_booking.save!
end
end
我仅更新 pending_bookings.each do |pending_booking|
pending_booking.host_status = 'expired'
pending_booking.requester_status = 'expired'
pending_booking.save!
end
end
和host_status
,但出现错误:
requester_status
,而此字段未更新。
所以我不确定是否真的了解发生了什么