【问题标题】:Ruby on Rails SystemStackError stack level too deep validate Wicked gemRuby on Rails SystemStackError 堆栈级别太深验证 Wicked gem
【发布时间】:2018-04-15 05:39:54
【问题描述】:

调用validate :method_name 会导致SystemStackError 无限循环。

直接内联进行验证允许验证通过而不会出错。

一定有什么我忽略或做错了......

直接在模型中而不是在conditional_reservation_validation 中进行验证时,验证效果很好。

导致SystemStackErrorstack level too deep的示例代码

根据这个例子构建了基本流程: Building Partial Objects Step by Step

我知道这需要重构/清理。

模型片段:

validate :conditional_reservation_validation

def conditional_reservation_validation
  if created_by_admin?
    validates_presence_of :vehicle_location
    validates_presence_of :pickup, :dropoff, :departure_date, :departure_time, :passengers
  else
    if active_or_parking?
      validates_presence_of :vehicle_location
      # SystemStackError occurs when validate :parking_agreement_if_location fires
      validate :parking_agreement_if_location
    end
    if active_or_pickup?
      # additional validations
    end
  end
  # ... 
end

def parking_agreement_if_location
  if vehicle_in_special_parking_location(vehicle_location)
    if self.parking_agreement == true
      # ok
    else
      self.errors.add :base, "Must read and understand parking instructions."
    end
  end
end

def vehicle_in_special_parking_location(vehicle_location)
  parking_locations = Location.where(require_parking: true)
  if parking_locations.include?(vehicle_location)
    return true
  else
    return false
  end
end

# methods to check the step in process
def active_or_parking?
  status.include?('parking') || active?
end

调用 validate :parking_agreement_if_location 会触发 SystemStackError

停止错误的示例代码: 只需将代码从 :parking_agreement_if_location 方法中取出并直接内联即可停止 SystemStackError。

validate :conditional_reservation_validation

def conditional_reservation_validation
  if created_by_admin?
    validates_presence_of :vehicle_location 
    validates_presence_of :pickup, :dropoff, :departure_date, :departure_time, :passengers
  else
    if active_or_parking?
      validates_presence_of :vehicle_location
      if vehicle_location
        locations = Location.where(require_parking: true)
        if locations.include?(vehicle_location)
          if parking_agreement == true
            # ok
          else
            self.errors.add :base, "Must read and understand parking instructions."
          end
          # validate :parking_agreement_if_location
        end
      end
    end

    if active_or_pickup?
      # These methods cause SystemStackError as well...
      validate :pickup_date_in_range
      validate :pickup_date_in_future
    end
  end
end

控制器更新操作:

def update

  params[:reservation][:status] = step.to_s
  params[:reservation][:status] = 'active' if step == steps.last

  case step
    when :parking
      @reservation.assign_attributes(reservation_params)
    when :pickup
      @reservation.assign_attributes(reservation_params)
    when :billing
      @reservation.assign_attributes(reservation_params)
  end
  render_wizard @reservation
end

【问题讨论】:

    标签: ruby-on-rails ruby wicked-gem


    【解决方案1】:

    您以错误的方式使用验证。它们需要在类级别调用。

    您需要改用条件验证:

    validates_presence_of :vehicle_location, if: :created_by_admin?
    validates_presence_of :pickup, :dropoff, :departure_date, :departure_time, :passengers, if: :created_by_admin?
    validates_presence_of :vehicle_location, unless: :created_by_admin?, if: :active_or_parking?
    validate :parking_agreement_if_location, unless: :created_by_admin?, if: :active_or_parking?
    

    【讨论】:

    • 谢谢 MikDiet - 我想我现在明白了 - 我需要使用 active_or_pickup 吗?检查管理员的方法并根据需要跳过。脑子放个屁!
    猜你喜欢
    • 2015-05-15
    • 2013-10-05
    • 2017-08-19
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多