Rails Console - Find where created at = certain day
You can do it like this:
date = Date.parse('january 5 2013')
users = User.where(created_at: date.midnight..date.end_of_day)
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.
Active Record - Find records which were created_at before today
Using ActiveRecord the standard way:
MyModel.where("created_at < ?", 2.days.ago)
Using the underlying Arel interface:
MyModel.where(MyModel.arel_table[:created_at].lt(2.days.ago))
Using some thin layer over Arel:
MyModel.where(MyModel[:created_at] < 2.days.ago)
Using squeel:
MyModel.where { created_at < 2.days.ago }
How to get record created today by rails activerecord?
Post.where(created_at: Time.zone.now.beginning_of_day..Time.zone.now.end_of_day)
PS: This answer has been modified as answer by Harish Shetty was better than mine. As my answer is accepted one. I have updated this answer for community support
Rails: Find tasks that were created on a certain day?
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!
Which DateTime method to use to fetch records for particular day?
You can use Date.today for getting records for a specific day like:-
date = Date.today
DailyDiagramsLog.where(:day => (date.beginning_of_day..date.end_of_day))
or
time = Time.now
DailyDiagramsLog.where(:day => time.beginning_of_day..time.end_of_day)
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.
How to query the active record filtering by date?
You need to parse the Date like so:
Student.where(status:'active', created_at: (Date.parse('2021-05-01')..Date.parse('2021-05-31'))).count
You could also let Rails set the beginning and end of the month:
Student.where(status:'active', created_at: (Time.new(2021, 5).beginning_of_month..Time.new(2021, 5).end_of_month))).count
If status ist an enum you can also query like this:
Student.active.where(created_at: (Time.new(2021, 5).beginning_of_month..Time.new(2021, 5).end_of_month))).count
Rails where date is greater than given date query
Rails 3+ :
Movie.where('release > ?', DateTime.now)
Pre Rails 3
Movie.where(['release > ?', DateTime.now])
Related Topics
Activerecord Objects in Hashes Aren't Garbage Collected -- a Bug or a Sort of Caching Feature
How to Define_Method in Rails Models
Rails: How to Write Tests for a Ruby Module
How to Log the Entire Trace Back of a Ruby Exception Using the Default Rails Logger
How to Read the Body Text of an Email Using Ruby's Net/Imap Library
Ruby: Initialize() VS Class Body
Rails Scope Find with Current User
Rails -- Create and Create! Methods, Ror 3 Tutorial
Gem::Loaderror: Specified 'Sqlite3' for Database Adapter, But the Gem Is Not Loaded
Token Based Authentication for Rails JSON APIs
Using a Ruby Script to Login to a Website via Https
Best Way to Generate Order Numbers for an Online Store
How to Recompile a Ruby with Rvm
How to Uninstall Ruby on Rails and Do a Clean Install
How to Make Object Instance a Hash Key in Ruby
Rails 4.2 - Sidekiq Not Sending Emails in Development
Ruby: How to Pass All Parameters and Blocks Received by One Method to Another