Rails: find jobs created on a specific day?

I have a list of tasks (name, starts_at), and I'm trying to display them in daily mode (like iCal).

def todays_tasks(day)
    Task.find(:all, :conditions => ["starts_at between ? and ?", day.beginning, day.ending]
end

I can’t understand how to convert Time.now, for example, "2009-04-12 10:00:00", to the beginning (and end) of the day dynamically so that I can make a comparison.

+5
source share
4 answers
def todays_tasks(now = Time.now)
  Task.find(:all, :conditions => ["starts_at > ? AND < ?", now.at_beginning_of_day, now.at_end_of_day])
end

Voila!

+11
source

Turning around on the August answer. In your model, Taskyou can define a method to extract these tasks for you.

  def self.todays_tasks(t = Time.now)
    Task.all(:conditions => ["created_at > ? AND created_at < ?", t.at_beginning_of_day, t.tomorrow.at_beginning_of_day])
  end

. . , 11:59:59 PM, .

+3

Is not it:

Task.all(:conditions => ["created_at >= ? AND created_at < ?", Time.now.at_beginning_of_day, Time.now.tomorrow.at_beginning_of_day] )

Or else you will never check 00:00:00 on the day of tasks, and 11:59:59 - the last value at the end of the day of tasks.

0
source
def todays_tasks(day)
   Task.where(:created_at => day.beginning_of_day .. day.end_of_day)
end
0
source

All Articles