如何更改下面的查询以仅选择过去 7 天内创建的记录?
self.favorites.count
此函数位于我的User
模型中。
def calculate_user_score
unless self.new_record?
self.score = (self.links.count * 5) + (self.favorites.count * 0.5)
end
end
最佳答案
您可以像这样添加一个where
条件:
self.favorites.where('created_at >= ?', 1.week.ago).count
对于您的 calculate_user_score
方法,您可能也想为 links
这样做:
def calculate_user_score
unless new_record?
self.score = (links.where('created_at >= ?', 1.week.ago).count * 5) +
(favorites.where('created_at >= ?', 1.week.ago).count * 0.5)
end
end
关于ruby-on-rails - 计算最近 7 天内创建的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8426781/