我有一个clicks
表,用于记录网站上链接的点击次数。
在links
表中,我想要一个可以通过cron作业更新的字段weekly_clicks
。
如何在Ruby / ActiveRecord中编写以下伪代码查询?
Select count(clicks) FROM clicks
WHERE link_id=xxx
AND created_at within previous 7 days including today
答案 0 :(得分:4)
Click.where(:link_id => 'xxx').where('created_at >= ?', 1.week.ago).count
答案 1 :(得分:2)
link = Link.new(params)
link.weekly_clicks = Click.where("created_at > ? and link_id = ?", 1.week.ago, xxx).count
link.save