Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails skip validation within model with save?

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.

like image 933
vidur punj Avatar asked Apr 19 '18 14:04

vidur punj


1 Answers

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

Try

.save(validate: false)
like image 160
Imre Raudsepp Avatar answered Nov 14 '22 01:11

Imre Raudsepp