Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.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/rails中的日期差异查询_Ruby On Rails_Ruby_Date - Fatal编程技术网

Ruby on rails ruby/rails中的日期差异查询

Ruby on rails ruby/rails中的日期差异查询,ruby-on-rails,ruby,date,Ruby On Rails,Ruby,Date,我有一个Opportunity模型,它有一个名为date\u of\u Opportunity的属性。我试图写一个查询,在那里我可以找到每一个日期在今天30天内的机会。我的问题是: Opportunity.where("date_of_opportunity - :today < 30", today: Date.today).size Opportunity.where(“Opportunity的日期-:today

我有一个Opportunity模型,它有一个名为date\u of\u Opportunity的属性。我试图写一个查询,在那里我可以找到每一个日期在今天30天内的机会。我的问题是:

 Opportunity.where("date_of_opportunity - :today < 30", today: Date.today).size
Opportunity.where(“Opportunity的日期-:today<30”,today:date.today)。大小
我的机会日期如下: 2018-05-06 2011-04-04 2009-04-08 2017-03-06

但是,当我运行查询时,它会返回所有日期。有人知道我的问题出在哪里吗?也许“30”不是天的单位


谢谢

嗨-我修改了我的日期,所以我有一个30天内的日期:2014-06-08。我修改了您对Opportunity的回答。where(Opportunity的日期:(30天以前)…时间。现在)。计数,但仍返回0。请告诉我。我只是输入了你写的内容,然后检查了控制台。这实际上返回了日期介于5/5和6之间的所有Opportunity/4@user1547174这不是你的目标吗?它还应该返回什么?如果你在一个命名的作用域中使用它,你需要使用lambda,否则它将始终返回相同的日期集
Opportunity.where(date_of_opportunity: (30.days.ago)..Time.now)