Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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 on rails 如何确定日期是否在一周范围内?_Ruby On Rails_Ruby_Date - Fatal编程技术网

Ruby on rails 如何确定日期是否在一周范围内?

Ruby on rails 如何确定日期是否在一周范围内?,ruby-on-rails,ruby,date,Ruby On Rails,Ruby,Date,我在字段中创建了一条记录。我想做的是知道在创建的是否在1-2周前创建,也就是说,第8、9、10、11、12、13、14天,而不是第1-7或15天 考虑到我无法在SQL中查询,Rails实现这一点的最佳方法是什么?这在Ruby 2.0.0和Rails 4.0上对我来说很有效 Record.created_at.between?(((Date.today - 14.days).beginning_of_day),(Date.today.end_of_day)) 你可以用一个靶场 Range.new

我在字段中创建了一条记录。我想做的是知道在创建的
是否在1-2周前创建,也就是说,第8、9、10、11、12、13、14天,而不是第1-7或15天


考虑到我无法在SQL中查询,Rails实现这一点的最佳方法是什么?

这在Ruby 2.0.0和Rails 4.0上对我来说很有效

Record.created_at.between?(((Date.today - 14.days).beginning_of_day),(Date.today.end_of_day))
你可以用一个靶场

Range.new((Date.today-14.days), 
   (Date.today-7.days)) === record.created_at
根据两周前的含义调整范围A

编辑: 这一行有点简练。把它分解

start_date = (Date.today - 14.days)
end_date = (Date.today - 7.days)

# Now start_date is 2 weeks ago, and end_date is 1 week ago.
#   Test if created_at falls within this range
Range.new(start_date.to_date, end_date.to_date) === record.created_at

假设2周前为八月十三日,1周前为八月二十日,而记录<<代码> CREATEDION 属性位于中间某个地方,这将返回<代码>真< /代码>。

谢谢,但根据上述Q,我没有查询数据库。我已经有一张记录了。在那里有一些奇怪的东西。范围=>2013年8月12日星期一..2013年9月2日星期一。。。我的created_at=>c.created_at.to_date=>2013年8月26日星期一---这没有意义,因为上面这行返回的是真的。它不应该返回真的吗?8月26日介于8月12日和9月2日之间。我想我看到了问题所在,我添加了+7.5天,而不是-7.5天。很抱歉。已修复。谢谢,但我需要2-3周的范围以记录创建时为基础。创建时,\u以便我知道此用户是在2-3周前创建的…2-3周范围,还是1-2周范围?谢谢Ryan,但几周前需要以记录为基础。创建\u atFYI,这会循环出现控制台错误“/Users/X/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.11/lib/active_support/time_with_zone.rb:332:warning:time#succ已过时;使用时间+1“请更好地解释你的问题。很明显,你的原始问题有很多歧义。为什么你想看看创建的时间是否在创建时间之前的1-2周内?
 ((2.weeks.ago)..(1.week.ago)).include?(record.created_at)