【问题标题】:Query By State Machine State Rails 3按状态机状态 Rails 3 查询
【发布时间】:2011-07-26 01:02:06
【问题描述】:

我正在使用 Transitions gem 在我的模型中创建一个状态机,如下所示:

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

但这似乎行不通,而且我很难找到这方面的文档。有谁知道如何做到这一点? (我敢肯定它可能只是找不到它)提前谢谢!

编辑

运行 tail -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/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'

运行 grep 'current_state db/schema.rb' 不会返回任何结果。我希望虽然没有称为 current_state 的实际列,但记录的状态由状态机管理(不确定它存储状态的确切位置)。

【问题讨论】:

  • tail -n 500 development.log, grep 'current_state db/schema.rb
  • 命令原样不起作用(我在 Leopard 上),但我将其更改为:tail -n 500 development.log | grep 'current_state db/schema.rb' 没有返回任何结果
  • 这是两个不同的系统命令
  • 用结果编辑了原始问题。谢谢你的帮助:)
  • 不要忘记进行迁移并运行 rake db:migrate 没有这样的列:batches.current_state

标签: ruby-on-rails state-machine transitions


【解决方案1】:

我也在研究状态机,虽然我还没有在我的项目中实现它,但我发现这可能会有所帮助。

  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

与 Rails 一起使用

这将进入您的 Gemfile:

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

……并将其添加到您的 AR 模型中:

包括 ActiveRecord::Transitions

关于持久性的说明

用于保存模型状态的属性名为 state(真的!),它应该是一个字符串列,其宽度足以容纳您最长的状态名称。还应该提到#save!在每次成功事件后调用。

希望对你有帮助,祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 2014-10-28
    • 1970-01-01
    相关资源
    最近更新 更多