我正在通过rails使用自定义验证程序,但我的验证程序始终创建记录但未通过save
检查。为什么?
customers_controller.rb
@coupon = Coupon.create(user_unique_identifier: params[:UDID], promo_code: params[:code])
unless !@coupon.save
##always fails here
coupon.rb
validate :one_per_customer, :before => :create
private
def one_per_customer
if self.is_paid_session
return true
else
@coupon = Coupon.where(user_unique_identifier: self.user_unique_identifier, promo_code: self.promo_code).first
if @coupon.present?
errors.add(:user_unique_identifier, 'PROMO CODE ALREADY APPLIED TO THIS DEVICE')
return false
else
return true
end
end
end