自动更改后的Rails的地位 [英] Automatically change status of post Rails

查看:113
本文介绍了自动更改后的Rails的地位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Rails应用程序我有我想要的状态将自动被管理员修改为已归档后的30天内批准这是可能的工作模式?如果是的话是什么做的呀?

In my rails application I have a job model which I want the status to be changed automatically to "archived" after 30 days from approval by the admin is this possible? if so what is the way to do it?

推荐答案

一个rake任务或后台作业(如Active作业或延迟工作)可以做的伎俩,但是你可能并不需要他们在这种情况下。如果你正在处理的时间戳在数据库中,您可以创建一个范围或一个方法,归档标记的工作。

A rake task or background job (such as Active Job or Delayed Job ) can do the trick however you might not need them in this case. If you are dealing with timestamps in your database, you can create a scope or a method to mark jobs as archived.

举例来说,如果你有一个名为列 approved_at 这是一个日期时间。当您批准工作,将 approved_at = Time.now

For instance, if you have a column named approved_at that is a datetime. When you approve a job, you set the approved_at = Time.now.

现在,您可以创建一个方法,表示如果作业存档:

Now you can create a method that indicates if the job is archived:

def is_archived?
  approved_at && approved_at < Time.now - 30.days
end

和让所有的归档工作,你可以创建一个范围:

And to get all the archived jobs, you can create a scope:

scope :archived, -> { where('approved_at IS NOT NULL AND approved_at <?', Time.now - 30.days)}

这篇关于自动更改后的Rails的地位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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