Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何使用Sequel获取当前月份_Ruby_Sqlite_Date_Sequel - Fatal编程技术网

Ruby 如何使用Sequel获取当前月份

Ruby 如何使用Sequel获取当前月份,ruby,sqlite,date,sequel,Ruby,Sqlite,Date,Sequel,我想用Sequel恢复当前月份的条目列表 我试过: Entry.where(:date >= Date.month).sum(:duration) 或 和其他方法,但它们似乎都不起作用。您需要考虑数据库的思维方式,以及Sequel如何将Ruby ranges转换为SQL: require 'date' today = Date.today # => #<Date: 2013-07-03 ((2456477j,0s,0n),+

我想用Sequel恢复当前月份的条目列表

我试过:

Entry.where(:date >= Date.month).sum(:duration)


和其他方法,但它们似乎都不起作用。

您需要考虑数据库的思维方式,以及Sequel如何将Ruby ranges转换为SQL:

require 'date'

today = Date.today                          # => #<Date: 2013-07-03 ((2456477j,0s,0n),+0s,2299161j)>
first_of_month = (today - today.day) + 1
first_of_month                              # => #<Date: 2013-07-01 ((2456475j,0s,0n),+0s,2299161j)>
next_month = today + 31                     # => #<Date: 2013-08-03 ((2456508j,0s,0n),+0s,2299161j)>
last_of_month = next_month - next_month.day # => #<Date: 2013-07-31 ((2456505j,0s,0n),+0s,2299161j)>
last_of_month                               # => #<Date: 2013-07-31 ((2456505j,0s,0n),+0s,2299161j)>

Entry.where(:date => [first_of_month .. last_of_month]).sum(:duration)
需要“日期”
今天=日期。今天=>#
每月第一天=(今天-今天.天)+1
月初#
下个月=今天+31#
最后一个月=下个月-下个月.day#=>#
最后一个月#
条目。其中(:日期=>[每月第一次..每月最后一次])。总和(:持续时间)
我会向您展示SQL输出,但我不知道您使用的数据库类型,而且,我很懒


通常,您可以在数据库中玩一些小把戏,截断“now”以删除日期,然后查找截断日期与其匹配的所有时间戳。这比使用Sequel对DBM更为具体,Sequel在将范围转换为“between”类型语句时已经知道如何处理范围。

如果您想要当前月份和当前年份的所有条目,可能最容易使用范围:

d = Date.today
Entry.where(:date=> Date.new(d.year, d.month)...(Date.new(d.year, d.month) >> 1)).sum(:duration)
如果您希望在任何一年中使用当前月份,Sequel内置了以下支持:

Entry.where(Sequel.extract(:month, :date) => Date.today.month).sum(:duration)
Entry.where(Sequel.extract(:month, :date) => Date.today.month).sum(:duration)