state-machines / state_machines

Adds support for creating state machines for attributes on any Ruby class
https://github.com/state-machines/state_machines
MIT License
814 stars 91 forks source link

Issue with Rails 4.2 #29

Closed epinault closed 9 years ago

epinault commented 9 years ago

I cannot seem to make it work with ActiveRecord. The initial state is never set correctly. I am using Ruby 2.1.x and Rails 4.2.x. Gem is 0.4.0

I have a Model that has a state field. State machine goes from pending, to starting to running to done

So pretty simple. But something is not working right (was working with 4.0 and the older version of state_machine (the one your forked from).)

So I am upgrading rails and finding that the old one is unmaintained and trying yours. But still not working properly for me

 f = FeedCrawlingRun.new
=> #<FeedCrawlingRun:0x007fc7b4ebccf0
 id: nil,
 started_at: nil,
 stopped_at: nil,
 state: nil,
 statistics: {},
 downloaded_files: {},
 feed_id: nil,
 created_at: nil,
 updated_at: nil>

Notice that state is nil . if I try to created it, it is never initialize correctly either

but if I do a f.starting? or f.pending? , it show the correct state. The state also shows with f.state.. But it is never saved to DB...

what am I missing?

epinault commented 9 years ago

Here also another session

 f = FactoryGirl.build(:feed_crawling_run)
=> #<FeedCrawlingRun:0x007fa6093e57b8
 id: nil,
 started_at: nil,
 stopped_at: nil,
 state: nil,
 statistics: {},
 downloaded_files: {},
 feed_id: 1002,
 created_at: nil,
 updated_at: nil>
[7] pry(main)> f.save
   (0.2ms)  BEGIN
  SQL (0.7ms)  INSERT INTO "feed_crawling_runs" ("feed_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["feed_id", 1002], ["created_at", "2015-09-09 21:52:40.467757"], ["updated_at", "2015-09-09 21:52:40.467757"]]
PG::NotNullViolation: ERROR:  null value in column "state" violates not-null constraint
DETAIL:  Failing row contains (5, null, null, null, , , 1002, 2015-09-09 21:52:40.467757, 2015-09-09 21:52:40.467757).
: INSERT INTO "feed_crawling_runs" ("feed_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"
   (0.2ms)  ROLLBACK
ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR:  null value in column "state" violates not-null constraint
DETAIL:  Failing row contains (5, null, null, null, , , 1002, 2015-09-09 21:52:40.467757, 2015-09-09 21:52:40.467757).
: INSERT INTO "feed_crawling_runs" ("feed_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"
from /home/preseed/.gem/ruby/2.1.5/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql_adapter.rb:606:in `exec_prepared'
[8] pry(main)> f.state
=> "pending"
epinault commented 9 years ago

Nevermind. Found a stackoverflow that points to an extra gem to install (the state_machines-activerecord one)