-
Notifications
You must be signed in to change notification settings - Fork 512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial state doesn't work in Rails 4.2 #334
Comments
I've solved problem by adding default value for rails g migration add_default_value_to_user_state class AddDefaultToUserState < ActiveRecord::Migration
def change
change_column_default :users, :state, :waiting_confirmation
end
end |
I got the same problem too, i'm using this (for now)
|
This monkey patch seems to work as an initializer, based on #335. But given that this gem seems unmaintained, I'm going to be looking for another alternative. # 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 |
State machine works fine in Rails 4.1.8. When I run my seed file "rake db:drop:all db:create db:migrate db:seed --trace" in rails 4.2 is get this error: ActiveRecord::RecordInvalid: Validation failed: State is invalid /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/validations.rb:79:in `raise_record_invalid' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/validations.rb:43:in `save!' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:29:in `save!' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:291:in `block in save!' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:347:in `block in with_transaction_returning_status' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:220:in `transaction' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:344:in `with_transaction_returning_status' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:291:in `save!' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/bundler/gems/state_machine-1a373b2d23a2/lib/state_machine/integrations/active_record.rb:491:in `block in save!' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/bundler/gems/state_machine-1a373b2d23a2/lib/state_machine/integrations/active_record.rb:506:in `block (2 levels) in around_save' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/bundler/gems/state_machine-1a373b2d23a2/lib/state_machine/transition_collection.rb:150:in `block in run_actions' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/bundler/gems/state_machine-1a373b2d23a2/lib/state_machine/transition_collection.rb:170:in `catch_exceptions' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/bundler/gems/state_machine-1a373b2d23a2/lib/state_machine/transition_collection.rb:148:in `run_actions' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/bundler/gems/state_machine-1a373b2d23a2/lib/state_machine/transition_collection.rb:133:in `run_callbacks' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/bundler/gems/state_machine-1a373b2d23a2/lib/state_machine/transition_collection.rb:212:in `run_callbacks' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/bundler/gems/state_machine-1a373b2d23a2/lib/state_machine/transition_collection.rb:63:in `block (2 levels) in perform' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/bundler/gems/state_machine-1a373b2d23a2/lib/state_machine/transition_collection.rb:63:in `catch' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/bundler/gems/state_machine-1a373b2d23a2/lib/state_machine/transition_collection.rb:63:in `block in perform' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/bundler/gems/state_machine-1a373b2d23a2/lib/state_machine/transition_collection.rb:186:in `within_transaction' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/bundler/gems/state_machine-1a373b2d23a2/lib/state_machine/transition_collection.rb:62:in `perform' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/bundler/gems/state_machine-1a373b2d23a2/lib/state_machine/integrations/active_record.rb:506:in `block in around_save' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/bundler/gems/state_machine-1a373b2d23a2/lib/state_machine/integrations/active_record.rb:534:in `block in transaction' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:220:in `transaction' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/bundler/gems/state_machine-1a373b2d23a2/lib/state_machine/integrations/active_record.rb:533:in `transaction' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/bundler/gems/state_machine-1a373b2d23a2/lib/state_machine/integrations/active_record.rb:505:in `around_save' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/bundler/gems/state_machine-1a373b2d23a2/lib/state_machine/integrations/active_record.rb:491:in `save!' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/fabrication-2.11.3/lib/fabrication/generator/base.rb:94:in `persist' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/fabrication-2.11.3/lib/fabrication/generator/base.rb:26:in `create' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/fabrication-2.11.3/lib/fabrication/schematic/definition.rb:75:in `block in fabricate' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/fabrication-2.11.3/lib/fabrication/schematic/definition.rb:74:in `instance_eval' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/fabrication-2.11.3/lib/fabrication/schematic/definition.rb:74:in `fabricate' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/fabrication-2.11.3/lib/fabricate.rb:29:in `create' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/fabrication-2.11.3/lib/fabrication.rb:62:in `Fabricate' /repos/derail/db/seeds.rb:12:in `' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:547:in `load_seed' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:250:in `load_seed' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:180:in `block (2 levels) in ' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `call' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `each' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `each' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:75:in `run' /home/saimon/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rake-10.4.2/bin/rake:33:in `' /home/saimon/.rbenv/versions/2.1.5/bin/rake:23:in `load' /home/saimon/.rbenv/versions/2.1.5/bin/rake:23:in `' Tasks: TOP => db:seed I installed state machine via Gemfile: gem 'state_machine', git: 'https://github.com/seuros/state_machine.git' The code where this happens: repo = Fabricate(:repo, project: project, server_url: 'CustomLogger', server_type: 'GitHub') Fabricate is same as FactoryGirl.create(:repo, ...) Repo is an AcriveRecord Model which has this state code: state_machine :state, initial: :ready do state :retrieved state :pulled state :removed event :retrieve do transition ready: :retrieved transition removed: :retrieved end event :pull do transition retrieved: :pulled transition pulled: :pulled transition compleated: :pulled end event :remove do transition retrieved: :removed transition pulled: :removed transition compleated: :removed end end |
@SaKKo @kalashnikovisme thanks for posting workarounds! |
confirmed this issue. |
+1 |
If the gem is not maintained anymore, maybe someone wants to maintain its fork? |
@SaimonL +1 |
@pluginaweek any news on this? Thanks. |
The solution is you have to set default state in the database level. t.string :status, default: 'start' Having the state nil will cause error. Don't know about the updated versions. |
This worked for me in development, but not in production on Heroku. |
Guys, just use state_machines-activerecord. |
I use https://github.com/seuros/state_machine with Rails 4.2 everything work now |
+1 |
2 similar comments
+1 |
+1 |
@mkonikowski @shlima @khaleksa |
@styx I've discovered it already, but forgot to comment here... Maybe in the Readme you could add info that this repo is not maintained anymore and put a link to |
@mkonikowski Nobody has access to this repo anymore. That why we have the other one :) |
@seuros - thanks, I am now using state_machines-activerecord |
+1 |
Hi, I have problem with initial state in new Rails 4.2
Any ideas?
The text was updated successfully, but these errors were encountered: