Iterating months using ruby - ruby-on-rails

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

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

Rails last day of last week bug?

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

how to get weak days of a particular week for a selected month and year in rails?

I want to get all the weak days of a particular week, for the selected year and month. I am getting data like year= 2015, month= 6 ,and weeknumber = 2 in controller. how to get week days for this particular data in rails?
i want a method like this which outputs the weekdays by taking week number. but the week number should be for that month like it should be less than or equal to five. below code outputs the week number for the whole year.
require 'date'
def week_dates( week_num )
year = Time.now.year
week_start = Date.commercial( year, week_num, 1 )
week_end = Date.commercial( year, week_num, 7 )
week_start.strftime( "%m/%d/%y" ) + ' - ' + week_end.strftime("%m/%d/%y" )
end
puts week_dates(22)
Because you're using rails you have the active support methods for beginning_of_week and end_of_week documented here you can do this
# first week in June
d = Date.new(2015,6, 1)
# => Mon, 01 Jun 2015
# add a week to get the second week
d += 1.week
# => Mon, 08 Jun 2015
(d.beginning_of_week..d.end_of_week).to_a
#=> [Mon, 08 Jun 2015, Tue, 09 Jun 2015, Wed, 10 Jun 2015, Thu, 11 Jun 2015, Fri, 12 Jun 2015, Sat, 13 Jun 2015, Sun, 14 Jun 2015]
If you want your week to start on sunday you can do this, passing the beginning day of the week:
(d.beginning_of_week(:sunday)..d.end_of_week(:sunday)).to_a
#=> [Sun, 31 May 2015, Mon, 01 Jun 2015, Tue, 02 Jun 2015, Wed, 03 Jun 2015, Thu, 04 Jun 2015, Fri, 05 Jun 2015, Sat, 06 Jun 2015]

In the context of rails, how can I order columns by the second?

I am working with rails and postgresql. I have a created_at timestamp on a model. I am in a situation where I need to order the results by second. I can't seem to get rails todo this.
e.g. If I insert 5 records in 1 minute, and then want to sort by created_at time, since they all have the same minute, it starts to order them by alpha because they all have the same insert minute.
How can I make it so that it orders by the insert in seconds. Postgres is storing it. But DateTime doesn't seem to take seconds into account in this way...
I hope this makes sense
Kirk
ActiveRecord order method should sort including the second
Eg:
Note the last two datetimes are in the same minute
User.pluck(:created_at)
=> [Fri, 14 Mar 2014 16:26:02 UTC +00:00, Fri, 14 Mar 2014 18:33:42 UTC +00:00, Fri, 14 Mar 2014 18:33:51 UTC +00:00]
User.order("created_at desc").collect(&:created_at)
=> [Fri, 14 Mar 2014 18:33:51 UTC +00:00, Fri, 14 Mar 2014 18:33:42 UTC +00:00, Fri, 14 Mar 2014 16:26:02 UTC +00:00]
Adding:
default_scope -> { order('created_at DESC') }
to your ActiveRecord should do it because created_at should look like this:"2014-03-12 15:29:26" which specifies the seconds.

Resources