在特定时间调用模型方法(Ruby on Rails) [英] Calling a model method at a specific time (Ruby on Rails)
问题描述
我有一个 Ruby on Rails 模型,其中有一列名为 expiration_date
.一旦达到到期日期,我希望修改模型上的另一列(例如 expired = true
).有什么好的方法可以做到这一点?
I have a Ruby on Rails model that has a column called expiration_date
. Once the expiration date is reached, I want another column on the model to be modified (e.g. expired = true
). What are some good ways of doing this?
理想情况下,我希望在达到到期日期的确切时刻调用模型函数.
Ideally I'd like a model function to be called at the exact moment the expiry date is reached.
推荐答案
对于您描述的场景,最好的解决方案是使用 expired
方法而不是列,如果expiration_date
大于或等于当前日期.
For the scenario you describe, the best solution is to have an expired
method instead of a column, that would return true iff the expiration_date
is greater or equal than the current date.
对于其他场景,我会使用触发存储过程的数据库计划事件.该过程将检查模型表中所有行的 expiration_date
列,并相应地更新 expired
(或其他(s))列.
For other scenarios, I would go with a DB scheduled event triggering a stored procedure. That procedure would check the expiration_date
column for all the rows in the model table, and update the expired
(or other(s)) column(s) accordingly.
这篇关于在特定时间调用模型方法(Ruby on Rails)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!