【问题标题】:Storing an unevaluated ActiveRecord query in DelayedJob在 DelayedJob 中存储未评估的 ActiveRecord 查询
【发布时间】:2010-12-10 04:05:42
【问题描述】:

我想安排一个延迟作业,其中包含将在作业实际运行时评估的活动记录查询(或条件)。

例如。我有一个自定义作业,它正在生成并向用户发送通知。 我想将参数/查询/条件发送到将过滤通知发送到的用户的作业。

我可以评估一个字符串,但这看起来很丑。

该项目使用的是 Rails 2.3.5,所以我不能走 Arel 路线。

我看到了有关 Ambition 的资料,但自 2008 年以来就没有了,所以我不确定该项目的状态。

建议?

【问题讨论】:

    标签: ruby-on-rails ruby activerecord delayed-job delayed-execution


    【解决方案1】:

    您可以在创建作业时将条件等作为哈希发送到作业,然后像通常使用 ActiveRecord 一样使用它。也许是这样的:

    class NotificationJob < Struct.new(:message, :query)
    
      def perform
        @users = User.all(query)
        ...
      end
    
    end
    

    然后你像这样创建作业:

    query = {:conditions => ["users.company_id = ?", @company.id]}
    Delayed::Job.enqueue(NotificationJob.new("There is no cake",query), 0, Time.now)
    

    如果这不适合您,那么也许您可以提供您当前的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 2013-03-09
      • 1970-01-01
      • 2010-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多