Callbacks not working.

53 views
Skip to first unread message

Gurpartap Singh

unread,
May 25, 2012, 9:37:26 PM5/25/12
to pluginaw...@googlegroups.com
I tried it as simple as `around_transition :perform_some_action` as well as after and before callbacks with :on => :some_event, and :from_state => :to_state.

No clue why the method isn't being called. Using Rails 3.2.3 on Postgres 9.1.3.
Message has been deleted

Gurpartap Singh

unread,
May 25, 2012, 10:06:20 PM5/25/12
to pluginaw...@googlegroups.com
I tried observers. The default active record callbacks are successfully invoked. However, state_machine's transition callbacks aren't. Kind of frustrated for having no clue where I'm doing it wrong.

class VehicleObserver < ActiveRecord::Observer
  # ActiveRecord callback.
  def after_save(vehicle)
    100.times { puts "This works!" }
  end

  # Generic state_machine transition callback *after* the transition is performed.
  def after_transition(vehicle, transition)
    100.times { puts "This doesn't work!" }
  end
end

And the observer is enabled with `config.active_record.observers = :vehicle_observer` in application.rb.

Gurpartap Singh

unread,
May 26, 2012, 12:11:02 AM5/26/12
to pluginaw...@googlegroups.com
Just realized that for the callbacks to be called, you must invoke them by updating the "state_event" param, instead of the "state" param.

Gurpartap Singh

unread,
May 26, 2012, 12:11:46 AM5/26/12
to pluginaw...@googlegroups.com
.. which requires an event as the value of the param, instead of the state name, as obvious.

Aaron Pfeifer

unread,
May 28, 2012, 9:20:50 AM5/28/12
to pluginaw...@googlegroups.com
Hey Gurpartap -

That's correct -- simply changing the state value doesn't trigger anything within state_machine because there's really no indication as to what event has caused that state to change.  To play it safely, you should always change states by using events so that the rules of the state machine are followed.

Glad you were able to figure this one out!

Best,
Aaron
Reply all
Reply to author
Forward
0 new messages