如何在rails中从基于数据库的created_at中选择数据

时间:2014-04-15 06:51:50

标签: ruby-on-rails arrays time days

如何在rails中从基于数据库的created_at中选择数据?

示例:

[#<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">]

当我今天收集基于created_at的数据时,我得到结果:

<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中收集数据created_at的所有数据?

之前感谢

5 个答案:

答案 0 :(得分:6)

请试试这个:

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

答案 1 :(得分:1)

要获取今天的events,它只会返回today's个事件

Event.where("created_at = ?", Date.today)

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

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">]> 

答案 2 :(得分:1)

请尝试使用此代码。

Event.where("created_at >= ? and created_at <= ?", Time.now.beginning_of_day, Time.now.end_of_day)

答案 3 :(得分:0)

Event.where("created_at > ?" Time.now.to_date.to_time)应该有用,因为你想要今天的参赛作品。另外,一个好主意是过去24小时的那个,应该是Event.where("created_at > ?-60*60*24 and created_at < ?" Time.now)

答案 4 :(得分:-1)

Event.where('created_at >= ?', Date.today)

也许您可以将created_at解析为相同的短格式并进行比较,但这应该足够了。