Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 如何从rails中创建的数据库中选择数据_Ruby On Rails_Arrays_Time_Days - Fatal编程技术网

Ruby on rails 如何从rails中创建的数据库中选择数据

Ruby on rails 如何从rails中创建的数据库中选择数据,ruby-on-rails,arrays,time,days,Ruby On Rails,Arrays,Time,Days,如何从今天在rails中创建的数据库中选择数据 例如: [#<Event id: 1, customer_id: 1, therapist_id: 1, location_id: 1, service_id: 3, walkin: true, cancel: false, cancel_reason: nil, room_id: 5, starts_at: "2014-04-03 21:31:13", stops_at: nil, created_at: "2014-04-14 10:13

如何从今天在rails中创建的数据库中选择数据

例如:

[#<Event id: 1, customer_id: 1, therapist_id: 1, location_id: 1, service_id: 3, walkin: true, cancel: false, cancel_reason: nil, room_id: 5, starts_at: "2014-04-03 21:31:13", stops_at: nil, created_at: "2014-04-14 10:13:52", updated_at: "2014-04-14 10:13:52">, #<Event id: 2, customer_id: 2, therapist_id: 2, location_id: 2, service_id: 2, walkin: false, cancel: false, cancel_reason: nil, room_id: 1, starts_at: "2014-04-05 21:31:13", stops_at: nil, created_at: "2014-04-14 10:13:52", updated_at: "2014-04-14 10:13:52">, #<Event id: 3, customer_id: 1, therapist_id: 2, location_id: 1, service_id: 1, walkin: false, cancel: false, cancel_reason: nil, room_id: 5, starts_at: "2014-04-07 21:31:13", stops_at: nil, created_at: "2014-04-14 10:13:52", updated_at: "2014-04-14 10:13:52">, #<Event id: 4, customer_id: 2, therapist_id: 2, location_id: 2, service_id: 2, walkin: false, cancel: false, cancel_reason: nil, room_id: 1, starts_at: "2014-04-09 21:31:13", stops_at: nil, created_at: "2014-04-14 10:13:52", updated_at: "2014-04-14 10:13:52">, #<Event id: 5, customer_id: 14, therapist_id: 2, location_id: 2, service_id: 1, walkin: true, cancel: false, cancel_reason: "", room_id: 8, starts_at: "2014-04-15 06:47:00", stops_at: "2014-04-15 07:47:00", created_at: "2014-04-15 06:47:14", updated_at: "2014-04-15 06:47:14">]
当我收集今天创建的数据时,我得到的结果是:

<Event id: 5, customer_id: 14, therapist_id: 2, location_id: 2, service_id: 1, walkin: true, cancel: false, cancel_reason: "", room_id: 8, starts_at: "2014-04-15 06:47:00", stops_at: "2014-04-15 07:47:00", created_at: "2014-04-15 06:47:14", updated_at: "2014-04-15 06:47:14">
如何在rails中创建数据的地方收集所有数据

谢谢你

Event.where('created_at >= ?', Date.today)
也许您可以将创建的\u at解析为相同的短格式并进行比较,但这应该足够了。

要获取今天的事件,它将只返回今天的事件

以下是我在控制台中尝试的内容

2.0.0p247 :008 > Article.where("created_at < ?", Date.today)
  Article Load (0.5ms)  SELECT `articles`.* FROM `articles` WHERE (created_at < '2014-04-15')
 => #<ActiveRecord::Relation [#<Article id: 2, title: "Villa Kerylos: A Greek Dream.. A Modern Villa", description: "An inside look at one of the Mediterranean's most i...", url: "http://frenchantiques.blogspot.com/2009/05/villa-ke...", is_open_in_new_window: true, created_at: "2013-12-26 07:03:17", updated_at: "2013-12-26 10:28:36">, #<Article id: 4, title: "Kenzo Auctions Personal Art Collection", description: "Kenzo Auctions Personal Art Collection", url: "http://frenchantiques.blogspot.com/2009/06/kenzo-au...", is_open_in_new_window: false, created_at: "2013-12-26 09:57:21", updated_at: "2013-12-26 09:57:21">]> 

请尝试使用此代码

Event.where("created_at >= ? and created_at <= ?", Time.now.beginning_of_day, Time.now.end_of_day)
请试试这个:

Event.where("DATE(created_at) = DATE(?)", Time.now)

Event.wherecreated_at>?Time.now.to_date.to_时间应该有效,因为您需要今天的条目。另外,一个好主意是过去24小时的时间,应该是Event.wherecreated_at>?-60*60*24和created_at<?时间。现在

这不可能起作用,因为他在创建时有一个时间对象。它起作用了,你可以在你这边测试它,请不要无缘无故地否决投票。好的。你是对的。我用常用的ruby代码尝试过:if Time.now>Date.today为真,否则为假。它不起作用。很抱歉,我对你的帖子投了赞成票。在正常的时间里,每天都没有这样的功能-class@user3383458检查此链接@RSB我已检查时间。现在。一天的开始。它返回2014-04-15 12:47:24+0200:Time NoMethodError的“未定义的方法开始日期”。。。此函数用于DateTime2.0.0p247:018>Time.now.Start\u of_day=>2014-04-15 00:00:00+0530@user3383458,Time.now.一天的开始应该有用。。奇怪的是,这对你来说不起作用。
Event.where("DATE(created_at) = DATE(?)", Time.now)