Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

ruby on rails - AASM Callback right after creating and instance

so im using the aasm gem for the first time and im emulating a credit card transaction.

The initial state is "Pending" and i need a callback that right after creating a transaction it checks if the amount of the transaction is bigger than the limit. If its lower then transition from pending to paid, and if not, from pending to failed.

The after_transaction callback is used when transitioning right? It wouldn't work here. I would need a callback that calls the method "approve_transaction" right after the object is created.


aasm :column_name, column: 'status' do # default column: aasm_state
    state :pending, initial: true
    state :paid, :failed

    after

    event :fail do
      transitions from: :pending, to: :failed
    end

    event :approve do
      transitions from: :pending, to: :paid
    end

  def approve_transaction
    if limit_left
      approve
    else
      fail
    end
  end

  def limit_left
    remaining_limit = self.credit_card.limit - self.amount
    remaining_limit >= 0 ? true : false
  end


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I would try using an active record callback (eg, after_create) to call approve_transaction

here's the api


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...