在rails中七天后更改内容

时间:2013-12-03 05:30:59

标签: ruby-on-rails ruby ruby-on-rails-3

假设我有一个模型说记录。有一些记录在表中说10。现在我想显示每个记录七天并更改它 例如:
1. AA。
2. BB。
3. CC。
4. DD。
等等。
前七天我希望在完成七天后显示AA我想隐藏AA amd show BB,然后隐藏BB并显示CC等等。
简而言之,我需要一次只显示一个数据七天,然后用下一个数据进行更改。任何帮助都会非常感激!!

3 个答案:

答案 0 :(得分:2)

class YourController < ActionController
  START_DATE = Date.parse("3/12/2013")
  MAX_RECORDS  = 10 # Set this value to the number of records you have

  def entry_index_to_display
    today = Date.today
    interval = (today - START_DATE).to_i
    index = (interval/7.0).floor
    #index = (index>index.floor)? (index.floor + 1) : index
    index % MAX_RECORDS
  end

  def the_method
    offset = entry_index_to_display
    @record = Record.limit(1).offset(offset).first
    # Find the record using this index.
    # Use this record in the view to display

  end
end

答案 1 :(得分:1)

我想你想要构建类似于系统的东西,在最初几周内向新用户显示不同的广告或提示。我会使用哈希来配置它,因为它易于阅读和理解:

DISPLAY_RULES = { 7.days => 'AA', 14.days => 'BB', 21.days => 'CC', ... }

def current_advertising
  rule = DISPLAY_RULES.find { |days, item| days.ago < current_user.created_at }

  Advertising.where(name: rule.last) if rule
end

答案 2 :(得分:0)

您还可以使用计划程序并每天运行它。在表中添加一个名为“active”的列,它将让您知道哪一个处于活动状态,然后递增到下一个。

为此,您可以使用whenever gem