【问题标题】:Schedule publishing of a post in a rails 3 blog安排在 rails 3 博客中发布帖子
【发布时间】:2011-10-28 11:18:59
【问题描述】:

我正在开发一个网站(使用 Rails 3.1),其中有限的“作家”能够写帖子。 “版主”应接受(或拒绝)帖子并安排发布时间。到目前为止,开发过程是非常基本的。

每天有两个发布时刻。接受的帖子将被放置在某种队列中。每天上午 10:00 和下午 4:00 必须发布最早接受的帖子。 但是,我还需要能够**手动设置**帖子上线的日期和时间。

实现结果的最佳方法是什么?克朗?后台作业?

【问题讨论】:

    标签: cron ruby-on-rails-3.1 queue scheduled-tasks background-process


    【解决方案1】:

    所以...

    1) 有一个accepted_at 字段,也可以手动设置;是时候“上线”了。

    2)

    class Post
      scope :ready_to_be_published, lambda{ where(['accepted_at<? and not published', Time.zone.now]).order('accepted_at ASC') }
    
      def accept!(time_to_go_live = nil)
        update_attributes!(:accepted_at => time_to_go_live || Time.zone.now)
      end
    end
    

    3) 在上午 10 点和下午 4 点有一个 whenever 工作来运行 rake 任务

    task :publish_a_post => :environment do
      Post.ready_to_be_published.first.update_attributes!(:published => true)
    end
    

    【讨论】:

    • 或者你可以有单独的accepted_atgo_live_at字段,所以实际接受时间也会被记录下来。
    • 哇!谢谢!有些事情只要简单,就是最好的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    相关资源
    最近更新 更多