Passing variables to Rails StateMachine gem transitions

前端 未结 5 2143
猫巷女王i
猫巷女王i 2021-02-05 10:07

Is it possible to send variables in the the transition? i.e.

@car.crash!(:crashed_by => current_user)

I have callbacks in my model but I nee

5条回答
  •  广开言路
    2021-02-05 10:46

    I used transactions, instead of updating the object and changing the state in one call. For example, in update action,

    ActiveRecord::Base.transaction do
      if @car.update_attribute!(:crashed_by => current_user)
        if @car.crash!()
          format.html { redirect_to @car }
        else
          raise ActiveRecord::Rollback
      else
        raise ActiveRecord::Rollback
      end
    end
    

提交回复
热议问题