30

I have Rails code like:

def charge_card
  return charge_on_house if house_account?
  assign_order_number
  if credit_card?
    begin
      save!   #==>here
      charge = Stripe::Charge.create(
        amount: (total.to_f * 100).ceil,
        currency: 'usd',
        customer: customer.stripe_id,
        card: payment_method,
        description:"Saint Germain Order: #{self.number}"
      )
      self.update(
        payment_status: 'paid'
      )
      self.finish!
    rescue Stripe::StripeError => e
      self.update(
        admin_comments: e.message,
      )
      self.decline!
    ensure
      notify_user
    end
  end
  self.save!
end

I want to skip validation on save! on line no 6 rather it is raising the error messages.

Roman Alekseiev
  • 1,854
  • 16
  • 24
vidur punj
  • 5,019
  • 4
  • 46
  • 65

1 Answers1

76

With save! validations always run. If any of them fail ActiveRecord::RecordInvalid gets raised.

Try

.save(validate: false)
zishe
  • 10,665
  • 12
  • 64
  • 103
Imre Raudsepp
  • 1,068
  • 8
  • 8