2011-07-26 30 views
0

我使用的轉換寶石創造在我的模型狀態機設置像這樣:查詢由狀態機狀態的Rails 3

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  

我要查詢的模式來獲取有所有的記錄某些國家如:

Batch.where :current_state => :pending 

但是,這似乎並沒有工作,我無法在此找到文檔。有誰知道如何做到這一點? (我相信這可能只是無法找到它)在此先感謝!

編輯

運行尾-n development.log給我:

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 @pending_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/[email protected]/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/[email protected]/gems/actionpack-3.0.9/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (6.6ms) 
Rendered /Users/dshipper/.rvm/gems/[email protected]/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' 

運行的grep 'current_state DB/schema.rb' 返回任何結果。我期望,因爲沒有實際的列名爲current_state,記錄的狀態由狀態機管理(不確定它確切存儲狀態的位置)。

+0

tail -n 500 development.log,grep'current_state db/schema.rb – Anatoly

+0

該命令不起作用(我在Leopard上),但我將其更改爲:tail -n 500 development.log | grep'current_state db/schema.rb'並且沒有返回結果 – dshipper

+0

這是兩個不同的系統命令 – Anatoly

回答

0

我還在研究狀態機,雖然我還沒有在我的項目中實現它,但我發現這可能會有所幫助。

  1. 確保您的Gemfile包括

    gem "transitions", :require => ["transitions", "active_record/transitions"] 
    
  2. 確保你的模型具有

    include ActiveRecord::Transitions 
    

    ,而不是僅僅

    include Transitions 
    

    ,它有一個名爲「列狀態'這一點是國家堅持的地方。


的問題似乎已經記錄在這裏:

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

而且在GitHub上的文檔還概述了Rails的

http://github.com/qoobaa/transitions

使用設置w ^第i個Rails的

這進入你的Gemfile:

寶石 「過渡」,:要求=> [ 「轉換」, 「active_record /轉換」]

......這到您的AR模型:

包括的Act​​iveRecord ::轉變

關於持久性的注意事項

用於保持模型狀態的屬性被命名爲state(真的!),它應該是一個足夠寬的字符串列,以適應您最長的狀態名稱。還應該提到#save!在每一次成功事件後都會被調用

希望這是有益的,祝你好運!