遍历状态-范围?红宝石/铁轨 [英] Loop through states - scope? ruby/rails

查看:85
本文介绍了遍历状态-范围?红宝石/铁轨的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够根据模型的当前状态来遍历该模型.

I'd like to be able to loop through the model based on its current state.

我为模型创建了非常简单的状态,如下所示:

I've created fairly simple states for my model as follows:

models/job.rb

class Job < ActiveRecord::Base
  has_many :events, class_name: "JobEvent"

  STATES = %w[bids in_progress complete canceled]
  delegate :bids?, :in_progress?, :complete?, :canceled?, to: :current_state

  def current_state
    (events.last.try(:state) || STATES.first).inquiry
  end 
end

models/job_event.rb

    class JobEvent < ActiveRecord::Base
      belongs_to :job
      attr_accessible :state

      validates_presence_of :job_id
      validates_inclusion_of :state, in: Order::STATES

      def self.with_last_state(state)
        order("id desc").group("job_id").having(state: state)
      end
    end

状态有效-我可以更改状态.我希望能够根据其当前状态遍历模型.不确定执行此操作的最佳方法.我是作用域的新手,正尝试按以下方式遍历我的状态(不起作用):

States works - I can change the state just fine. I'd like to be able to loop through the models based on its current state. Not sure about the best way to go about this. I'm new at scoping and am attempting to loop through my states as follows (not working):

views/index.html.erb

    <% @current_state_jobs.each do |job|  %>
        <tr>
          <td><%= link_to job.job_number, job %></td>
        </tr>
    <% end %>

似乎上面的方法可以工作-但出现以下错误:undefined method 'each' for nil:NilClass

Its seems like the above would work - but I'm getting the following error: undefined method 'each' for nil:NilClass

我应该专注于作用域还是将每个状态定义为控制器中的一种方法?也许我应该在循环的开头放置一个if then语句?有什么想法或推荐的读物吗?

Should I focus on scopes or maybe define each state as a method in the controller? Maybe I should put an if then statement at the beginning of the loop? Any ideas or recommended reading?

---更新---解决方案---

我最终在我的控制器索引方法中为每种状态添加了实例.然后在我的索引中添加一个选项卡式视图,以一次遍历每个状态.

I ended up adding instances in my controller index method for each state. Then added a tabbed view to my index to loop through each state at a time.

例如:

class JobsController < ApplicationController
  def index 
    @all = Job.where(:state => ['bids', 'in_progress', 'complete'])  #all job states
    @bids = Job.where(:state => ['bids']) 
    ... and so on for each state
    ...

推荐答案

好吧,您的undefined method错误是因为@current_state_jobs在您希望将其作为集合时为nil.您需要在控制器的index方法中为其分配一些值.

Well, your undefined method error is because @current_state_jobs is nil when you expected it to be a collection. You need to assign it some value in your controller's index method.

我不确定您的问题是否足够清楚.您是否要遍历所有Job实例并执行特定于每个状态的动作,还是要获取某个特定状态的所有作业并遍历这些状态?

I'm not sure your question is clear enough. Do you want to loop through all Job instances and perform actions specific to each one's state, or do you want to get all jobs for one particular state and loop through those?

如果是后者,则需要一个可用于获取该状态作业的范围或类方法.也许是这样的:

If it's the latter, you need a scope or class method that you could use to grab jobs for that state. Perhaps something like this:

class Job < ActiveRecord::Base
  def self.with_state(state)
    joins(:events).where("job_event.state = ?",state)
  end
end

@current_state_jobs = Job.with_state("in_progress")

(为此,您需要current_state实例方法,除非您在其他地方使用它).

(You need the current_state instance method for this, unless you're using it elsewhere).

这篇关于遍历状态-范围?红宝石/铁轨的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆