如何在 Rails 4.2 和 sqlite 中按星期几过滤? [英] How to filter by day of week in Rails 4.2 and sqlite?

查看:39
本文介绍了如何在 Rails 4.2 和 sqlite 中按星期几过滤?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何按每周的每一天检索记录?

How to retrieve records by each day of week ?

这是我的范围:

scope :day_of_week, ->(day_of_week) { where("created_at = ?", day_of_week ) }

我试过 strftime('%w', day_of_week) 和 DAYOFWEEK() 都没有成功.

I 've tried strftime('%w', day_of_week) and DAYOFWEEK() without success.

我使用几个月和几年都没有问题,我正在计算我认为的所有内容.我还在计算一周中的每一天的项目,即使我没有收到语法错误,我也会收到 0.

I am using months and years without a problem and I am counting everything on my view. I am counting also items per day of week and even If I am not getting a syntax error, I am receiving 0.

推荐答案

scope :day_of_week, ->(day_of_week) { where("strftime('%w', created_at) = ?", day_of_week.to_s) }

是解决方案.我不太确定为什么 .to_s 有效,但似乎文档不清楚.如果我们认为 strftime 的意思是字符串到时间类型",那么它应该需要一个字符串.

is the solution. I am not quite sure why .to_s worked but it seems that the documentation is not clear. If we consider that strftime means "String to Time type" then it should need a string.

这篇关于如何在 Rails 4.2 和 sqlite 中按星期几过滤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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