简体   繁体   English

在Ruby / Rails中使用if / then为当前状态创建范围

[英]Create scope for current state using if/then in Ruby / Rails

I'm trying to figure out how to create a scope for 'late' jobs. 我试图弄清楚如何为“后期”工作创建范围。

A 'late' job will be one where the state is :in_progress where the :due date is older than today. “迟到”的工作将是状态为:in_progress的工作,其中:due日期早于今天。

(I'm not using any state gems and have Postgres as the database). (我没有使用任何状态的gem,而将Postgres作为数据库)。

This is what I have going so far. 这就是我到目前为止所要做的。 I'm fairly new to if then statements and have only created basic scopes to this point. 我对if then语句还很陌生,并且到目前为止只创建了基本范围。 Any thoughts on how to go about this in my model? 在我的模型中对此有任何想法吗?

# states # 状态

  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

# scope # 范围

  def self.late
    if @current_state_jobs == :in_progress?
      where('due < ?', Time.now) 
    else
      nil 
    end
  end

Can I use the first current_state scope in the late scope? 我可以在后一个范围中使用第一个current_state范围吗? I'm not sure 我不确定

I'm not sure I fully understand your setup, but I can certainly help steer you in the right direction. 我不确定我是否完全了解您的设置,但是我当然可以帮助您将方向正确。

I don't believe your delegate statement will work because as far as I know it needs to set the :on option to an object of some sort and not a method. 我不相信您的委托语句会起作用,因为据我所知,它需要将:on选项设置为某种对象而不是方法。 In your case, what I think you need to delegate to is state.inquiry . 在您的情况下,我认为您需要委派给的是state.inquiry There's probably a way to do that, but I'm afraid I don't know how. 可能有一种方法可以做到,但是恐怕我不知道该怎么做。

Here is a quick discussion on delegates that seemed pretty helpful to me: 这是对代表的快速讨论,对我似乎很有帮助:

http://www.simonecarletti.com/blog/2009/12/inside-ruby-on-rails-delegate/ http://www.simonecarletti.com/blog/2009/12/inside-ruby-on-rails-delegate/

With that in mind, these delegate methods are still instance methods, and a scope needs class methods. 考虑到这些委托方法仍然是实例方法,并且作用域需要类方法。

I'm not sure what @current_state_jobs is supposed to be, but that's an instance variable and you're in a class function so I don't think it works. 我不确定@current_state_jobs应该是什么,但这是一个实例变量,并且您在一个类函数中,因此我认为它不起作用。 I think what you're really looking for is simply where(state: "in_progress") . 我认为您真正要寻找的只是where(state: "in_progress")

Next you want to do a query on the date which is actually kind of tricky. 接下来,您想对日期进行查询,这实际上有点棘手。 If you're not careful, Time.now sort of gets binded when the module is first loaded. 如果不小心,第一次加载模块时会绑定Time.now To prevent that, you need to use a lambda function to ensure the time is based on when the function is called. 为避免这种情况,您需要使用lambda函数来确保时间基于调用该函数的时间。

Also, you said in the description that you want the method to include anything that is "older than today", but what your query is doing is anything "older than right now". 另外,您在描述中说,您希望该方法包括“比今天更旧”的任何内容,但查询所要做的就是“比现在更旧”的任何内容。

To sum it all up I think you need something like this: 总结一下,我认为您需要这样的东西:

scope :late, lambda { where("state=? and due < ?", "in_progress", Date.today) }

If it were me, I'd probably break it into two different scopes like in_progress and overdue then have :late just be in_progress.overdue , but if you're not going to use either of those other two scopes then it's unnecessary. 如果是我,我可能会将其分为in_progressoverdue两个不同的范围,然后:late只是in_progress.overdue ,但是如果您不打算使用其他两个范围中的任何一个,那就没有必要了。

I hope that helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM