created_at date syntax problems - ruby-on-rails

I have the following piece of code:
humans = user.humans.joins(:human_logins).where(human_logins_count: 10).group('humans.id').having('MAX(human_logins.created_at) >= ?', Date.today() - schedule.value.day)
The problem with this is the created_at) >= ?segment as if my date is equal to 7 days ago, it will find the records that were made in the last 7 days, as opposed to finding the records that were created literally 7 days ago, not 6, not 9, exactly 7 days ago.
How can I make it so that its finding records created exactly 7 days ago? I was thinking of using something like ("? <= created_at AND created_at <= ?", schedule.value.days.ago.beginning_of_day, schedule.value.days.ago.end_of_day)but I'm not sure how I'd use it in this scenario.

How about this,
.group('human_logins.created_at').having('human_logins.created_at = ?', Date.today() - 7)

.having(created_at: schedule.value.days.ago.beginning_of_day.. schedule.value.days.ago.end_of_day )

Related

Rails find someones birthday [duplicate]

This question already has an answer here:
Compare Time.now in Rails but ignore year?
(1 answer)
Closed 4 years ago.
I have a birthday problem. I'm trying to find all users who have a birthday 5 days from now.
I can do something like the below but the year is causing problems..
#dobs = Time.now + 5.days
#users = User.where(dob: #dobs).all
I also tried to make the dob.year the current year but this breaks for birthdays in early Jan because 5 days earlier is the year before.
Is there a gem I can use, or is it something more obvious?
Try something like this:
User.where(dob: (Time.now + 5.days).all_day)
User.where(dob: (Date.today + 5.days).all_day) # or as Qwertie suggested
all_day will capture anything from the start to the end of the day.

Compare Time in Rails without the seconds

In my Rails 5 app I want to send an alert 10 minutes after a certain datetime in my database. For example 10 minutes after the created_at datetime.
So I run a cronjob every 1 minute and have to look in my database for all records where created_at was 10 minutes ago.
My created_at field is in the format 2017-06-01 12:00:00. How can I get all records where created_at was 10 minutes ago?
My problem are the seconds and I can't simple do (this is not valid Ruby code, it's just to show my point):
where created_at == 10.minutes.ago
I would have to do something like
where created_at > 10.minutes.ago AND created_at < 9.minutes.ago
But this does not seem very clean. Are there better solutions?
You have to provide range in order to get records..however, to avoid dependencies with seconds, you can do something like below elegantly..
range = range = 10.minutes.ago.beginning_of_minute..10.minutes.ago.end_of_minute
records = YourModel.where(created_at: range)

Show objects based on selected week

I have an active relation Bar object with an attribute shift_date. shift_date represents each day between March and June. March and June comes from Foo which has attributes start_month and end_month:
f = Foo.find(1)
days = (f.end_month - f.start_month).to_i
weeks = (days * 0.142857).round(2)
f.bars will give me days objects. Where days is the total amount of objetcs.
My trouble is to get Bars objects, objects for week 1, 2 or 3 etc:
f.bars.where('shift_date >= ?', (weeks/7.days)).group_by{ |result| result }
operator does not exist: timestamp without time zone >= numeric
So what am I saying? Give me all objects on week 1 or week 5, if any. How do I go about this, please?
Im on to something but not right:
f.bars.where('shift_date >= ?', Date.today).group_by{ |result| result}
Edit:
Im almost there. I could splat out the days with:
days_array = *(f.start_month..f.end_month)
then
f.bars.where(shift_date: days_array[0]..days_array[7])
That would be the answer! But...not really. For my views, I need to group the splatted days in a 7 days interval as week, so days_array[0] to days_array[7] would be week 1 and days_array[8] to days_array[14] would be week 2 etc. How to show that in the view? This will give me everything I need.

Google Spreadsheet, Query Date older than 5 years ago

I am trying to query a count of machines that are older than 5 years.
My machine data is in one tab with date time stamps in column K like "8/8/2008 8:08:08"
I have tried many different variations to get the query right... but my latest is like this
=query(RawDataMachines!B:K,"select count(K) WHERE K + INTERVAL 5 YEAR < NOW() label count(A1''")
Been trying to solve this issue for several days.
Any help would be appreciated.
Thanks in advance.
well try this and let me know....
select COUNT(K) from Machine where datediff(YEAR,k,getdate()) > 5;
datediff will return the number of years between the date k and the System Date.
Hope it Help's.

How do I determine if a date was within a week range?

I have a record with a created_at field populated. What I want to do is know if that created_at was created 1-2 weeks ago, meaning, days 8, 9, 10, 11, 12, 13, 14, not 1-7 or 15+.
Given that I can't query for this in SQL, what's the best Rails way to do this?
((2.weeks.ago)..(1.week.ago)).cover?(record.created_at)
Assuming 2 weeks ago was the 13th of August, 1 week ago is the 20th of August and the record's created_at attribute lies somewhere in the middle, this will return true.
This works for me on Ruby 2.0.0, Rails 4.0
Record.created_at.between?(((Date.today - 14.days).beginning_of_day),(Date.today.end_of_day))
You could use a range
Range.new((Date.today-14.days),
(Date.today-7.days)) === record.created_at
Adjust the range arugments depending on your meaning of two weeks ago.
EDIT:
The one liner is a bit terse. To break it down.
start_date = (Date.today - 14.days)
end_date = (Date.today - 7.days)
# Now start_date is 2 weeks ago, and end_date is 1 week ago.
# Test if created_at falls within this range
Range.new(start_date.to_date, end_date.to_date) === record.created_at
Updated answer for rails 7.0
Release Notes - Active Support Removals
Remove deprecated support to use Range#include? to check the inclusion of a value in a date time range is deprecated.
It's now recommended to use the cover? method.
For example:
((2.weeks.ago)..(1.week.ago)).cover?(record.created_at)

Resources