gpt4 book ai didi

ruby-on-rails - 在特定时间调用模型方法(Ruby on Rails)

转载 作者:行者123 更新时间:2023-12-04 06:28:16 26 4
gpt4 key购买 nike

我有一个 Ruby on Rails 模型,其中有一列名为 expiration_date .一旦达到到期日期,我希望修改模型上的另一列(例如 expired = true )。有什么好的方法可以做到这一点?

理想情况下,我希望在达到到期日期的确切时刻调用模型函数。

最佳答案

使用 delayed_job gem 。安装 delay_job gem 后,请执行以下操作:

class Model < ActiveRecord::Base

after_create :set_expiry_timer

# register the timer
def set_expiry_timer
delay(:run_at => expiration_date).expire
end

def expire
update_attribute(:expired, true) unless expired?
end

end

关于ruby-on-rails - 在特定时间调用模型方法(Ruby on Rails),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9934661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com