Rails last day of last week bug? - ruby-on-rails

I'm trying to get the last day of last week.
Today is:
d = Date.today
=> Sun, 22 Nov 2015
d.at_end_of_week
=> Sun, 22 Nov 2015
d.at_end_of_week.last_week
=> Mon, 09 Nov 2015
The last command should return Mon, 15 Nov 2015, no?
This also returns the same FYI:
Date.today.at_end_of_week.last_week
=> Mon, 09 Nov 2015
What am I missing here?

What you are seeing is correct. Note that last_week is an alias of prev_week which takes an optional parameter that defaults to Date.begining_of_week (:monday)
Resulting in trying to find the date in the previous week but from the past monday.
If instead you call Date.today.at_end_of_week.last_week :sunday. For what you try to accomplish you need a way to replace :sunday with something based on today's date. Maybe this but I haven't tried it.

Try:
Date.today.last_week.at_end_of_week

Related

I dont get the expenses of the 31 of the previous months with Activerecord in rails

When I show the total expend of PUQ theres no problem
Expend.where(cc: "PUQ").sum(:total)
But when I want to show by month individually with this code, I Don't get the expenses of the 31, only show 1 to 30 of the month. Right now I dont get the expenses of 31-08-2020
Expend.where(cc: "PUQ").where(:created_at => ((Date.today.beginning_of_month)- 1.month)..((Date.today.end_of_month) - 1.month)).sum(:total)
Thanks in advance
You can use these handy Rails methods
range = Time.current.advance(months: -1).all_month
# => Sat, 01 Aug 2020 00:00:00 UTC +00:00..Mon, 31 Aug 2020 23:59:59 UTC +00:00
Expend.where(created_at: range)
More about all_month
and about advance

How to get the date of "this Wednesday", "this Thursday" etc. in Rails

I know that Rails has an elegant way of getting dates for a certain day of the week for next week, or for last week (docs).
today = Time.zone.today
=> Fri, 24 Jan 2020
today.next_week
=> Mon, 27 Jan 2020 # Gets Monday by default
today.next_week(:thursday)
=> Thu, 30 Jan 2020 # You can get a certain day of the week by passing it in as an argument
Is there a way to do the same for this week? For example, getting the date of this Thursday, Friday, etc.? Right now, my workaround is to get the date of beginning_of_week (Monday) and calculate from there. Ideally, I wanted to do something similar to above, like Time.zone.today.this_week(:thursday).
this_monday = Time.zone.today.beginning_of_week
=> Mon, 20 Jan 2020
this_friday = this_monday.since(4.days).to_date
=> Fri, 24 Jan 2020
You could use #next_occurring
pry(main)> Time.zone.today.beginning_of_week
Mon, 20 Jan 2020
pry(main)> Time.zone.today.beginning_of_week.next_occurring(:thursday)
Thu, 23 Jan 2020

Active relation order not ordering correctly

I have records I need to order with a date attribute. In my code I call:
records.order("`records`.date esc")
however this doesn't seem to work.
If I run:
records.order("`financial_transactions`.date asc").pluck(:date)
I get:
[Fri, 09 Dec 2016, Wed, 07 Dec 2016, Sun, 25 Jun 2017, Sat, 25 Mar 2017]
which is obviously incorrect. I also get the same result if I run:
records.order("`records`.date desc").pluck(:date)
How can I fix this?
If you are on Rails 4+, this should work for descending order:
records.order(date: :desc).pluck(:date)
And if you want them in ascending order, you can just do:
records.order(:date).pluck(:date)
For Rails 3.2:
records.order('records.date desc').pluck(:date)
records.order('records.date asc').pluck(:date)
If you are on Rails 5, try
records.order(date: :desc).pluck(:date)
If you want them ascending, do this:
records.order(date: :asc).pluck(:date)

How to get time x days ago from a specific date in Rails?

I am looking for a rails solution to calculate the time ago from a particulat time. For example , 2 days ago 15th May 2016 22:00 UTC should return 13th May 2016 22::00 UTC .
My requirement is something like this
2.days.ago.from(yesterday)
Which will be a more specific version of
2.days.from_now
Try this:
> DateTime.now-2.days
=> Wed, 18 May 2016 21:40:31 -0700
how about this:
# 2 days before a specific date
specific_date.days_ago(2)
Example:
specific_date = DateTime.now
two_days_ago_from_specific_date = specific_date.days_ago(2)
My personal favorite syntax for this with rails would be
x.days.ago
For example, if you wanted 10 days ago you would call
10.days.ago
=> Sat, 12 Feb 2022 01:36:58 UTC +00:00
Easy to read and defaults to UTC.
example:
Time.now.ago(10.year)
Time.now.ago(1.minutes)
Time.now.ago(10.day)

Iterating months using ruby

I have a very weird requirement where I need to deal with months. Here is what I am try to do actually. I have two object say
jan_start_date=Time.parse("2012-01-01 00:00:00")
jan_end_date=Time.parse("2012-01-31 23:59:59")
I take this two datetime objects and iterate over feb, mar, april and soon to get some data.
Then I will take feb start and end dates and iterate over march, april and soon.
Its like month on month data collection. Once I am done with say Jan data, I need to take Feb's data and so on and so forth. How do I achieve this. Since I need to iterate over months.
Kindly help me out
Thanks
ActiveSupport has beginning_of_month and end_of_month:
d = Date.today
#=> Fri, 13 Jul 2012
d.beginning_of_month
#=> Sun, 01 Jul 2012
d.end_of_month
#=> Tue, 31 Jul 2012
You can use Date#>> to shift dates forward monthwise:
(d>>2).end_of_month
#=> Sun, 30 Sep 2012
(d>>4).beginning_of_month
#=> Thu, 01 Nov 2012

Resources