I'm working on an event system where admins can select events to feature and add promotional text. As they need to add additional text the featured events are in their own table and reference the event details.
I'm now trying to output featured events for each day in the next week. Simplified example:
day = DateTime.now.to_date
featured_events = #featured_events.where('event.start_datetime = ?', day)
The problem is that start_datetime is in datetime format and day is in date format so it always outputs nothing. Is it possible to compare these two values with .where()?
Thanks!
May be you're looking for this:
#featured_events.where('event.start_datetime > ? and event.start_datetime < ?', DateTime.now, 1.day.from_now.beginning_of_day)
You don't say what database you're using.
In PostgreSQL we'd truncate the datetime inside the DB query so the DBM can do the compare. On MySQL we'd use a function to convert the datetime to a date.
Related
Using Rails 6.
I have an ElectricityUsage model, with a Date field, date. I want to extract all the values for amount only for the current month. How would I accomplish this?
What I immediately attempted was the following:
ElectricityUsage.where(habitat: current_user.reservations[0].room.habitat).where(date.month: Date.today.month)
But that obviously doesn't work, and it wouldn't even account for the year, either. My DB is running on PostgreSQL, if that makes a difference.
You can use where with Date.current.all_month, which basically is just translated into a query using BETWEEN where the start date is the first day of the month and end date is the last one:
ElectricityUsage.where(habitat: current_user.reservations[0].room.habitat, date: Date.current.all_month)
This should work for you
ElectricityUsage.where(habitat: current_user.reservations[0].room.habitat).where("EXTRACT(MONTH FROM date) = ?", Date.current.month)
PostgreSQL Date/Time Functions and Operators
Might be a delayed response but you can use date_queries gem
model ElectricityUsage < ActiveRecord::Base
date_queries_for :date
end
Then you can simply use ElectricityUsage.dates_in_this_month to find all the records that false in current month
I have a model named Event. I want to be able to get all Events that were created at some specific date which should be something like this
Event.where(created_at: "20/01/2019".to_date)
The issue is that the created_at field also contains minutes, second, etc, which I want to ignore. I only want to compare the date by day, month and year. Is there any way I can acchieve that?
You can use Date#beginning_of_day and Date.end_of_day both provided by ActiveSupport core extensions.
d = '2019-01-20'.to_date
Event.where(created_at:(d.beginning_of_day..d.end_of_day))
This will not necessarily compare "...the date by day, month and year." but instead it will create a between clause that encompasses the entire day. If you want to use the Hash finder syntax this is probably the best way to go about it.
You could go with
Event.where(
Arel::Nodes::NamedFunction.new('CAST',
[Event.arel_table[:created_at].as('DATE')]
).eq(d)
)
If you really only want to compare the day, month and year.
Option 1 will result in
events.created_at BETWEEN '2019-01-20 00:00:00' AND '2019-01-20 23:59:59'
Option 2 will result in
CAST(events.created_at AS DATE) = '2019-01-20'
I'm trying to query items based on an association but getting a bit confused with how to phrase the date part of my query. The dates are stored in text format like "2018-12-25".
year = params[:ridden_in]
#items = Item.joins(:cycle).where(cycles: { (:date.to_date).year: year })
Anyone able to help out where i'm going wrong?
The easiest way to do this is to use SQL functions. You could try to hide the SQL behind AREL but that very quickly becomes an incomprehensible mess.
One way would be straight string manipulation:
Item.joins(:cycle).where('substring(cycles."date" from 1 for 4) = ?', year)
or slightly more future-proof since your date will eventually become a real date:
Item.joins(:cycle).where('extract(year from cycles."date"::date) = ?', year)
Item.joins(:cycle).where('extract(year from cast(cycles."date" as date)) = ?', year)
Both of those assume that your date columns really do follow the ISO8601 date format.
If you're doing this a lot then you could add a partial index on cycles.date (i.e. index the result of whichever function you end up using).
I want to be able to list any 'events' in my database which occur on the same date as another event being creating in my method.
The column I want to compare is 'starts_at' and is a DateTime.
How can I just compare the date that events occured on instead of both date and time?
I need to add the query to this where call:
events = Event.where(location_id: location.id)
I already have a date object date to compare to, I just don't know how I can get just the date part of the events already in the database?
is it something along the lines of date(starts_at:)?
events = Event.where(location_id: location.id, date(starts_at:): date)
I am using sqlite database.
Check if this works with your DB or not:
events = Event.where(location_id: location.id).where("DATE(starts_at) = ?", date)
OR, compare in datetime as per active record:
events = Event.where(location_id: location.id,starts_at: date.beginning_of_day..date.end_of_day)
Lets say I want to compare the dates only of two datetime columns within 1 record. So I don't want the time looked at.
I.e.
viewed_date, and updated_at (I added viewed_date) are two datetime formats, but I only want to see if they occurred on the same day or days apart. The problem with datetime is that its comparing the times, which is just too specific for me right now.
Thanks
-Elliot
Declare a new attribute that contains just the date:
class WhateverModel < ActiveRecord::Base
...
def viewed_date_only
viewed_date.to_date
end
end
Use that for your comparison in the controller or wherever.
You can compare only date of object without comparing time like this:-
start_date :-"2015-04-06 15:31:43 +0530"
end_date :- "2015-04-16 15:31:43 +0530 "
post_review.all.where("(created_at::date >= :start_date) AND
(created_at::date <= :end_date)", start_date: start_date.strftime("%Y-%m-%d"),end_date: end_date.strftime("%Y-%m-%d"))
Above query gets all the records made between 6 to 16 April