伯爵在过去7天内创建的记录 [英] Count records created within the last 7 days

查看:118
本文介绍了伯爵在过去7天内创建的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以改变下面,只选择在过去7天内创建的记录查询?

  self.favorites.count
 

该功能位于我的用户模式。

 高清calculate_user_score
    除非self.new_record?
      self.score =(self.links.count * 5)+(self.favorites.count * 0.5)
    结束
  结束
 

解决方案

您可以添加其中, -condition是这样的:

  self.favorites.where(created_at> =',1.week.ago).Count之间
 

和您的 calculate_user_score 方法,你可能想这样做的链接以及

 高清calculate_user_score
  除非new_record?
    self.score =(links.where('created_at> =?',1.week.ago).Count之间* 5)+
      (favorites.where('created_at> =?',1.week.ago).Count之间* 0.5)
  结束
结束
 

How can I alter the query below to only select records created within the last 7 days?

self.favorites.count

This function is located in my User model.

 def calculate_user_score
    unless self.new_record?
      self.score = (self.links.count * 5) + (self.favorites.count * 0.5)
    end
  end  

解决方案

You can add a where-condition like this:

self.favorites.where('created_at >= ?', 1.week.ago).count

And for your calculate_user_score method, you probably want to do that for links as well:

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  

这篇关于伯爵在过去7天内创建的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆