验证失败时,Create不会回滚

时间:2017-10-03 03:44:26

标签: ruby-on-rails validation

方法控制器中的代码(reservations_controller.rb):

 def create_reserve
        @reservation = Reservation.create(reservation_params)

        respond_to do |format|
          user = User.find_by_email(@reservation.email)
          if !user
            format.html {redirect_to reserve_path(:lpn => @reservation.lpn), notice: 'Customer does not exist'}
          end

          if @reservation.save
            checkout_timer @reservation.expect_start_time, @reservation.id, @reservation.lpn
            @car = Car.find_by_lpn(@reservation.lpn)
            if @car.status == "Available"
              @car.status = "Reserved"
              @car.update_attribute(:status, "Reserved")
            end
            @reservation.update_attribute(:status, "Reserved")
            format.html {redirect_to @car, notice: 'Reservation was successfully made.'}
            #   format.json { render :show_reserve, status: :reserved, location: @reservation }
          else
            format.html {redirect_to reserve_path(:reserve => {:email => @reservation.email, :lpn => @reservation.lpn}), notice: @reservation.errors.full_messages[0]}
          end
        end
      end

模型中的代码(reservation.rb):

validate :timeValidation, on: :create
validate :timeValidation, on: :update

def timeValidation
  if expect_start_time < Time.now || expect_start_time > Time.now + 7.days
    errors.add(:expect_start_time, "should be within 7 days from now")
  elsif  expect_return_time < Time.now || expect_return_time <= expect_start_time
    errors.add(:expect_return_time, "should be later than expect start time!")
  elsif expect_return_time - expect_start_time > 10.hours
    errors.add(:expect_return_time, :expect_start_time, "Total rental time should be no more than 10 hours.")
  end
end

在函数中,当@ reservation.save执行时,将调用Reservation模型中的验证函数。我希望在验证失败时,Reservation.create(reservation_params)会回滚。然而,真正发生的是这样的:

enter image description here

在@ reservation.save之前,创建已经提交。但我有validate :timeValidation, on: :create。它应该回滚。为什么呢?

2 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为你添加了两个:

CloudFront Distribution:
  Type: Web
  Origin Settings:
    Origin Domain Name: abc.mydomain.com
    Origin Id: CrossRegionDomain
  Distribution Settings:
    Alternate Domain Names (CNAMEs): xyz.mydomain.com

多次调用验证会覆盖先前的验证,请参阅https://github.com/rails/rails/blob/e0d2207ab321145c117c6d615ce209c6f873605d/activemodel/lib/active_model/validations.rb#L152

如果要对创建和更新事件执行验证,请执行以下操作:

xyz.mydomain.com

同时执行validate :timeValidation, on: :create validate :timeValidation, on: :update 而不是validate :timeValidation

答案 1 :(得分:0)

为什么要使用create创建@reservation并再次调用save ??????

更改方法如下

def create_reserve
  @reservation = Reservation.new(reservation_params)   // change to new
  ...
  if @reservation.valid? && @reservation.save          // check if its valid?
    ....
  end
end

如果问题仍然存在,请尝试以下

validate :timeValidation, on: :create
validate :timeValidation, on: :update
def timeValidation
  ...
  ...
  throw(:abort) if errors.present?        // raise error if error present.  
end