0

我有这样的徽章规则;

grant_on 'comments#create', badge: 'frequent-commenter', to: :user, temporary: true do |comment|
  comment.user.comments.where(:created_at.gte => (Date.today - 30)).count >= 20
end

用户在发送评论后将丢失徽章。但是,如果她/他不发送评论,她/他将永远拥有徽章。

是否可以检查临时徽章并删除不再有效的徽章?

4

1 回答 1

1

不能从优点规则运行,因为comment局部变量不会来自 cron 作业。我要做的是将逻辑移动到用户模型,然后从规则和 cron 作业中调用它:

grant_on 'comments#create', badge: 'frequent-commenter', to: :user,
  temporary: true do |comment|

  comment.user.is_frequent_commenter?
end

class User
  def is_frequent_commenter?
    comments.where(:created_at.gte => (Date.today - 30)).count >= 20
  end
end

class RemoveInvalidBadgesJob
  def perform
    # Better done by direct database query?
    User.all.reject(&:is_frequent_commenter?).map do |user|
      user.rm_badge(1)
    end
  end
end
于 2014-08-26T04:15:32.157 回答