用户可以评论,他们的评论是可以投票的。
每个用户都有一个业力/分数。
目前,我正在向用户展示他们的个人资料上的业力。我实际上没有把他们的分数保存在数据库里。我只是用以下方法来计算:
<% comment_vote_count = current_user.comments.map{|c| c.votes.count}.sum
comment_vote_count ||=0
comment_vote_count *= 2
total_vote_count = comment_vote_count + current_user.base_score %>base_score充当可以手动更改的锚点。
我想在得分机制中增加一些东西。我想为每一个实际接受邀请和注册的用户添加10分。
因此,计算用户业力的新逻辑应该是,在评论上收到的票数乘以2,再加上每次成功邀请的10分,再加上基本分数。
更重要的是,如何将其保存在数据库中?我想在用户表中添加一个名为score的列。但我该如何更新呢?我是否让控制器在每次用户加载他们的配置文件时更新它?
发布于 2014-04-16 23:22:43
这是一个简单的计划。首先,将score列添加到users表中。
在您的User模型中:
after_invitation_accepted :increment_score_of_inviter
def increment_score_of_inviter
invitation_by.increment!(:score, 10)
end
def comment_vote_count
Vote.find(comment_ids).count * 2
end
def calculated_vote_count
base_score + comment_vote_count
end
def recalculate_score!
update_attribute(:score, calculated_vote_count)
end如果您有大量的投票活动,这可能会非常低效率,但是要保持score列的更新,将其添加到您的Vote模型中:
after_create :increment_user_score
def increment_user_score
user.increment!(:score, 2)
end这里的另一种选择是定期重新计算score列,也许使用whenever gem:
every :hour do
User.find_each { |u| u.recalculate_score! }
endhttps://stackoverflow.com/questions/23121743
复制相似问题