pluginaweek / state_machine

Adds support for creating state machines for attributes on any Ruby class
http://www.pluginaweek.org
MIT License
3.74k stars 505 forks source link

fix issue initial state doesn't work in active_record 4.2 #335

Open fly1tkg opened 9 years ago

fly1tkg commented 9 years ago

This PR is for https://github.com/pluginaweek/state_machine/issues/334 .

seuros commented 9 years ago

The initial state won't persist to the database.

destino92 commented 8 years ago

Hello guys how do i apply this fix to the state-machine gem

shushugah commented 5 years ago

Hey Destino92, if you you want to monkey patch it, you can create a file

config/initializers/state_machine.rb

with

# Hacks around https://github.com/pluginaweek/state_machine/issues/334
module StateMachine
  module Integrations
    module ActiveRecord
      def define_state_initializer
        define_helper :instance, <<-end_eval, __FILE__, __LINE__ + 1
          def initialize(*)
            super do |*args|
              self.class.state_machines.initialize_states self
              yield(*args) if block_given?
            end
          end
        end_eval
      end
    end
  end
end