Find Records with Datetime That Match Today's Date - Ruby on Rails

Find Records with Datetime that match today's date - Ruby on Rails

Keep in mind a DateTime holds a date and a time, so you're looking for records that have a precise value, not just the same day.

You can do this one of two ways. You can either select the range of time from the beginning of the day to the end, or you can create a date column to help group your data better.

The range version looks like this:

@deals = Deal.where('start BETWEEN ? AND ?', DateTime.now.beginning_of_day, DateTime.now.end_of_day).all

The other one requires creating a new start_date column in your table and populating it with the dates accordingly. You can index this date and have your queries run much faster, as range selections aren't always quick on large sets of data.

Check if a datetime field is equal to Date.today

.where("DATE(assignments.due_date) = ?", Date.today)

How can I find records from today, yesterday and so on with Ruby on Rails?

Try this:

#Today
Posts.find(:all, conditions: { :created_at => Date.today...Date.today + 1 })
#Yesterday
Posts.find(:all, conditions: { :created_at => Date.today - 1...Date.today })

Or this (preferable, in my opinion):

#Today
Posts.find(:all, conditions: ["DATE(created_at) = ?", Date.today] )
#Yesterday
Posts.find(:all, conditions: ["DATE(created_at) = ?", Date.today - 1] )

query where date = Date.today with Rails, MySQL, and Active Record

I think this is a DUP of this question:

How to select date from datetime column?

Subscription.where("DATE(created_at) = ?", Date.today).count

I'm pretty sure this works in MySQL and PostgreSQL, but I'm not sure if it's a SQL standard.

Wikipedia seems to think TO_DATE would be the standard:
http://en.wikipedia.org/wiki/SQL#Date_and_time

That didn't work for me in PostgreSQL though.

Rails Active Record query against just the date field of the created_at datetime value

A simple trick is to use a BETWEEN clause, achievable without writing pure SQL and just by using ActiveRecord:

Count
.where(channel_id: channel_id)
.where(created_at: Date.yesterday.beginning_of_day..Date.yesterday.end_of_day)

It will translate to the following SQL:

WHERE ("counts"."created_at" BETWEEN '2019-01-02 00:00:00.000000' AND '2019-01-02 23:59:59.999999')

Compare date on a datetime column in rails active record

Something like this

# convert date to datetime
date = "2020-05-01".to_datetime

So you can do:

User.where(created_at: date.beginning_of_day..date.end_of_day)

With the comment below, it becomes even shorter:

User.where(created_at: created_at: date.all_day)

date.all_day will give you something like

Sun, 17 May 2020 00:00:00 UTC +00:00..Sun, 17 May 2020 23:59:59 UTC +00:00

Rails ActiveRecord Find / Search by Date

selected_date = Date.parse(params[:selected_date])
# This will look for records on the given date between 00:00:00 and 23:59:59
sh = SupportHistory.where(
:created_at => selected_date.beginning_of_day..selected_date.end_of_day)

Time Zones may be a concern you need to look into, but this should work if all your times are in the same time zone.

Rails group records by dates of created_at retrieve hash with date string as the key

You should cast date to a string, otherwise rails will format date.

Model.group("to_char(date(created_at), 'yyyy-mm-dd')").count

This should give an output hash as you require

Rails - get records that match a particular wday

You'll have to use a database specific function to achieve what you're looking for. Using Postgres you can achieve what you're looking for by doing the following:

@timesheet.sheets.where("to_char(date, 'day') = ?", Date.today.strftime('%A').downcase)

Since you will be manually creating a SQL string, you should be sure to prefix the column name with the appropriate table name in order to avoid column name collisions should there be other tables joined in the query.



Related Topics



Leave a reply



Submit