Ruby 中的简单自定义范围
我有一个Rails name_scope,它使用一个条件从表中提取一周中的特定日期,如下所示:
:conditions => [ 'EXTRACT(DOW FROM bookdate) IN (?)', (1..6).to_a ]
1..6 日期范围将是一个变量,具体取决于用户想要的日期,
这会产生此 SQL
(EXTRACT(DOW FROM bookdate) IN (1,2,3,4,5,6)
我的问题是一周中的几天不是一个简单的范围...即 1..6 工作正常(周一...周六),但说 6..2 将无法正常工作(周六-周二)...或者作为红宝石范围或因为它需要是 6,7,1,2 而不是 6,5,4,3,2 (假设 6..2 在 ruby 中有效,但事实并非如此)。
如何为一周中的几天创建一个自定义范围来容纳这样的日期范围?
有什么想法吗?
谢谢,
I have a Rails named_scope which is using a condition to pull specific days of the week from the table like so:
:conditions => [ 'EXTRACT(DOW FROM bookdate) IN (?)', (1..6).to_a ]
The 1..6 date range will be a variable depending on the dates the user wants,
Which produces this SQL
(EXTRACT(DOW FROM bookdate) IN (1,2,3,4,5,6)
My problem is that the days of the week are not a simple range... ie 1..6 works fine (Mon...Sat), but say 6..2 will not work correctly (Sat-Tues)... either as a ruby range or as it would need to be 6,7,1,2 and not 6,5,4,3,2 (assuming 6..2 worked in ruby, which it doesn't).
How can I create a custom range for days of the week that would accommodate a date range like this?
Any ideas?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要一个函数生成包含在
:conditions
中的值范围,该范围适用于周六至周二以及周一至周六等范围,怎么样:例如
那么这只是映射的情况如果需要,可以将日期名称转换为日期数字。
If you want a function to generate the range of values for inclusion in
:conditions
that will work for ranges like Sat-Tues as well as Mon-Sat how about:e.g.
Then it is just a case of mapping the day names to the day numbers if required.