Rails + Stripe: Enum status stays nil after update and save

24 Views Asked by At

I'm encountering a problem with updating the status attribute of an order in a Ruby on Rails application. Despite using the update method and saving the record, the status remains nil. The enum declaration for the status attribute seems correct, and there are no apparent errors in the code.

class PaymentsController < ApplicationController
  def show
    @order = Order.find_by(
      session_id: session.id.to_s,
      stripe_checkout_id: params[:session_id]
    )
    stripe_session = Stripe::Checkout::Session.retrieve(params[:session_id])
    if stripe_session.status == "complete"
      @order.paid!
      puts "Order marked as paid!"
    else
      @order.pending!
      puts "Order marked as pending!"
    end
  end
end

The logs shows "Order marked as paid!", and the payment goes through. But the status remains nil.

#<Order id: 34, session_id: "...", stripe_checkout_id: "...", status: nil, created_at: "2024-01-19 10:24:54.239392000 +0000", updated_at: "2024-01-19 10:53:40.805197000 +0000", line_item_id: nil>

order.rb

class Order < ApplicationRecord
  has_many :line_items
  has_many :candles, through: :line_items

  enum status: {
    pending: 0,
    paid: 1
  }
end
0

There are 0 best solutions below