开发者

Query By State Machine State Rails 3

开发者 https://www.devze.com 2023-03-23 00:56 出处:网络
I\'m using the Transitions gem to create a state machine in my model set up like so: require \'transitions\'

I'm using the Transitions gem to create a state machine in my model set up like so:

require 'transitions'

class Batch < ActiveRecord::Base
  include Transitions

  state_machine do
    state :pending
    state :completed


    event :change_state do 
      transitions :to => :completed, :from => [:pending] 
    end

  end


end    

I want to query the model to get all records that have a certain state e.g.:

Batch.where :current_state => :pending

But that doesn't seem to work, and I'm having trouble finding documentation on this. Does anyone know how to do this? (I'm sure it's possible just can't seem to find it) Thanks in advance!

EDIT

Running tail -n development.log gives me:

ActionView::Template::Error (SQLite3::SQLException: no such column: batches.current_state: SELECT "batches".* FROM "batches" WHERE "batches"."user_id" = 1 AND "batches"."current_state" = 'pending'):
    3: <%= link_to 'New Batch', new_batch_path %>
    4: 
    5:                                                                      
    6: <% unless @p开发者_如何学Goending_batches.length < 1 %>
    7:  You have <%= @pending_batches.length %> batches pending on these urls:
    8:  <% @pending_batches.each do |batch| %>
    9:      <%= batch.url %>        
  app/views/batches/index.html.erb:6:in `_app_views_batches_index_html_erb___355556540_17724200__911230187'
  app/controllers/batches_controller.rb:8:in `index'

Rendered /Users/dshipper/.rvm/gems/ruby-1.9.2-p180@artsicle/gems/actionpack-3.0.9/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.5ms)
  Batch Load (0.5ms)  SELECT "batches".* FROM "batches" WHERE "batches"."user_id" = 1 AND "batches"."current_state" = 'pending'
SQLite3::SQLException: no such column: batches.current_state: SELECT "batches".* FROM "batches" WHERE "batches"."user_id" = 1 AND "batches"."current_state" = 'pending'
Rendered /Users/dshipper/.rvm/gems/ruby-1.9.2-p180@artsicle/gems/actionpack-3.0.9/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (6.6ms)
Rendered /Users/dshipper/.rvm/gems/ruby-1.9.2-p180@artsicle/gems/actionpack-3.0.9/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (16.1ms)
  SQL (0.2ms)   SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'

  Batch Load (0.3ms)  SELECT "batches".* FROM "batches" HAVING "batches"."current_state" = 'pending'
SQLite3::SQLException: a GROUP BY clause is required before HAVING: SELECT "batches".* FROM "batches" HAVING "batches"."current_state" = 'pending'

And running grep 'current_state db/schema.rb' return no results. I would expect that though because there's no actual column called current_state, the state of the record is managed by the state machine (not sure exactly where it's storing the state).


I was researching state machines also and, though I haven't implemented it yet in my project, I found this that might be helpful.

  1. Make sure your gemfile includes

    gem "transitions", :require => ["transitions", "active_record/transitions"]
    
  2. Make sure your model has

    include ActiveRecord::Transitions
    

    rather than just

    include Transitions
    

    and that it has a column called 'state' as this is where state is persisted.


The problem seemed to have been documented here:

http://dev.netizer.pl/transitions-state-machine-for-rails-3.html/comment-page-1#comment-41

And the documentation on github also outlines the setup for Rails

http://github.com/qoobaa/transitions

Using with Rails

This goes into your Gemfile:

gem "transitions", :require => ["transitions", "active_record/transitions"]

… and this into your AR model:

include ActiveRecord::Transitions

A note about persistence

The property used to persist the models’ state is named state (really!), which should be a string column wide enough to fit your longest state name. It should also be mentioned that #save! is called after every successful event.

Hope this is helpful, good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消