ActiveModel-验证失败

时间:2019-01-08 17:48:07

标签: ruby-on-rails ruby activemodel

当我尝试更新字段时出现错误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_statusrequester_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,而此字段未更新。

所以我不确定是否真的了解发生了什么

0 个答案:

没有答案