本文介绍了Rails - 每当gem - 动态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个像这样的cronjob:

 每1.day,:at => '4:30 am'do 
runnerMyModel.task_to_run_at_four_thirty_in_the_morning
end

虽然,我想让'1.day'更具动态性,例如通过管理页面中的表单更改值。



然后它可能看起来像这样:

 每个Constant.find(2).value,:at => '4:30 am'do 

  @const = Constant.find(2)
every @ const.span,:at => @ const.time do

任何人都可以想出如何使这项工作? p>

显然,这样做的原因是我可以使用存储在我的网站数据库中的值,例如

 <%=下一次更新时间少于#{@const.time}#or something similar%> 


解决方案

我从来没试过,通过在任何时候加载Rails环境,以便可以使用您的模型类来执行此操作。只需使用 require File.dirname(__ FILE__)+./environment(或./ application



但是,因为所有的任何时候都会在crontab中生成行,所做的任何更改到任何常数将需要每次--update-crontab 时运行


Lets say I have a cronjob like this:

every 1.day, :at => '4:30 am' do
  runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
end

Although, I would like to make '1.day' more dynamic, such as changing the value via a form in an administration page.

Then it could look like this:

every Constant.find(2).value, :at => '4:30 am' do

or

@const = Constant.find(2)
every @const.span, :at => @const.time do

Can anyone come up with an idea on how to make this work?

Obviously, the reason for this would be that I could use the values stored in the database on my site, like an message saying

<%= "The next update is in less than #{@const.time}" #or something similar %>
解决方案

I've never tried, but you should be able to do this by loading up the Rails environment in whenever so that you can use your model classes. Just use require File.dirname(__FILE__) + "./environment" (or "./application" for Rails 3) in your schedule.rb, assuming your schedule.rb is in the config dir.

However, since all whenever does is generate lines in the crontab, any changes made to any Constant would require running whenever --update-crontab again.

这篇关于Rails - 每当gem - 动态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:50