DateTime object + hours|days|months from string method - ruby-on-rails

I have problem with adding time values to DateTime object. Controller action gets values from form:
task_form.start_at - DateTime
task_form.remind_value - String, e.g. "2"
task_form.remind_space - String, e.g. "minutes", "hours", "days"
I want to send method like
remind_time = task_form.start_at + send("#{task_form.remind_value}.#{task_form.remind_space}")
but I get NoMethodError: undefined method '2.days'. There is any way to use method like that?

You are trying to invoke the method '2.days' on self (the controller, I guess), but you need to call the 'days' method on the integer object returned by task_form.remind_value
Try this:
remind_time = task_form.start_at + task_form.remind_value.send(task_form.remind_space)

Rails provides a Date#advance method:
start_at = DateTime.new(2014, 9, 18)
remind_value = "2"
remind_space = "days"
start_at.advance(remind_space.to_sym => remind_value.to_i)
#=> Sat, 20 Sep 2014 00:00:00 +0000

Related

get 11 pm of every day as datetime

How we can set datetime which gives same time but different date on each day.
I want to get all objects which are created after 23:00:00 of each day
I have used string interpolation but can this be done in better way.
2.5.1 :013 > created_after = Date.today.strftime("%F") + " 23:00:00"
=> "2020-02-12 23:00:00"
You can try this -
created_after = DateTime.now.in_time_zone(Time.zone).beginning_of_day - 1.hour
or
created_after = DateTime.current.beginning_of_day - 1.hour
Here's another way of doing it:
created_after = DateTime.current.change({hour: 23, min: 0})

Creating a "this month" query rails

I am trying to create a query to record my submissions for this month in my controller using the following code:
#today = Time.now
#thismonth = #today.month
#nextmonth = #thismonth + 1
#submissions = SubmissionLocation.includes(:submission).where(location_id: current_agent.Company_Business_Location, location_id: 2134).map(&:submission)
#submissions_by_group = #submissions.group_by { |m| m.Max_move_in_date.beginning_of_month}.sort_by{:Max_move_in_date}
#submission_this_month = #submissions_by_group.where(:Max_move_in_date => #thismonth)
It seems to be throwing a nomethod error for the where condition in the #submission_this_month instance variable. I understand that it is most likely because my data is now in a hash format, so in that case, how I would I then query it?

New DateTime instead of String in ruby

I've got some issue with DateTime in Ruby
I've got line which looks like this (it's in .txt file)
DateTime.new(1979,1,1) DateTime.new(2012,3,29)
And my function to get this looks like this
def split_line
array = line.split(' ')
#date_of_birth = array[0]
#date_of_death = array[1]
end
But #date_of_birth and #date_of_death class are String. How can I get them as DateTime?
Assuming your string is in the correct format, then you're probably looking for:
#date_of_birth = array[0].to_datetime
#date_of_death = array[1].to_datetime
See here for more info:
https://apidock.com/rails/String/to_datetime
This:
DateTime.new(1979,1,1) DateTime.new(2012,3,29)
Is not code. What do you expect that to do?
If you want two DateTimes as a space-separated string, do something like:
"#{DateTime.new(1979,1,1)} #{DateTime.new(2012,3,29)}"
When you have something like #{...} inside a set of double quotation marks (they must be double, not single quotation marks), it's called string interpolation. Learn it. Love it. Live it.
But, for the life of me, I don't know why you wouldn't do:
[DateTime.new(1979,1,1), DateTime.new(2012,3,29)]
Which gives you an array, so no split needed. Just:
def split_line
#date_of_birth = array[0]
#date_of_death = array[1]
end
If you want DateTime values, grab the numbers and create them:
require 'date'
'DateTime.new(1979,1,1) DateTime.new(2012,3,29)'.split.map { |s|
DateTime.new(*s.scan(/\d+/).map(&:to_i) )
}
# => [#<DateTime: 1979-01-01T00:00:00+00:00 ((2443875j,0s,0n),+0s,2299161j)>,
# #<DateTime: 2012-03-29T00:00:00+00:00 ((2456016j,0s,0n),+0s,2299161j)>]
The values aren't DateTime though, they're Dates:
'DateTime.new(1979,1,1) DateTime.new(2012,3,29)'.split.map { |s|
Date.new(*s.scan(/\d+/).map(&:to_i) )
}
# => [#<Date: 1979-01-01 ((2443875j,0s,0n),+0s,2299161j)>,
# #<Date: 2012-03-29 ((2456016j,0s,0n),+0s,2299161j)>]
Breaking it down:
'DateTime.new(1979,1,1) DateTime.new(2012,3,29)'.split # => ["DateTime.new(1979,1,1)", "DateTime.new(2012,3,29)"]
.map { |s|
Date.new(
*s.scan(/\d+/) # => ["1979", "1", "1"], ["2012", "3", "29"]
.map(&:to_i) # => [1979, 1, 1], [2012, 3, 29]
)
}
# => [#<Date: 1979-01-01 ((2443875j,0s,0n),+0s,2299161j)>,
# #<Date: 2012-03-29 ((2456016j,0s,0n),+0s,2299161j)>]
* (AKA "splat"), used like this, explodes an array into its elements, which is useful when you have an array but the method only takes separate parameters.
The bigger question is why you're getting values like that in a text file.

Argument error : invalid date

What this error could be about? I'm defining an age at my account model:
def age
birthday = DateTime.new(year_Of_Birth.to_i, month_Of_Birth.to_i, day_Of_Birth.to_i)
self.age = ((DateTime.now - birthday) / 365.25).to_i
end
and in my view i get the following error:
ArgumentError - invalid date in the following line:
<dd><%= #account.age %></dd>
Thanks in advance.
Try to test in your Rails app console:
birthday = DateTime.new(1991,4,2) => Tue, 02 Apr 1991 00:00:00 +0000
DateTime.now - birthday => (149073456141953/17280000000)
(DateTime.now - birthday)/365.25 => 23.61926437561592
((DateTime.now - birtday)/365.25).to_i => 23
This works for me. Check the params that you're setting.
I'm almost sure the arguments that you're passing (year_Of_Birth.to_i, month_Of_Birth.to_i, day_Of_Birth.to_i) have something wrong, debug them and show us the values, or if you want you can try this to catch some specific error:
begin
birthday = DateTime.new(year_Of_Birth.to_i, month_Of_Birth.to_i, day_Of_Birth.to_i)
self.age = ((DateTime.now - birthday) / 365.25).to_i
rescue ArgumentError
#your logic
end

Ruby how can i get date of Monday after a given date

Given A Day i-e Monday and a Date e.g: Sat, 09 Aug 2014
how can i get the date of Monday coming immediately after given Date
Update:
def get_date_after(date, day)
return date if date.wday == day.to_date.wday
days_difference = (date - day.to_date).to_i
result = day.to_date + days_difference + (day.to_date.wday - date.wday)
result = result + 1.week if result.to_date < date
end
I was looking for something like above method, calling it would return the date of the day passed coming immediately after the date passed
get_date_after(DateTime.parse("15/09/2014").to_date, "Wednesday") #=> 17/09/2014
The chronic gem allows you to parse date expressions, e.g. Chronic.parse("next Monday"). You can add a reference date with the now option. Here's how it could be used for your method:
def get_date_after(date, day)
Chronic.parse("next #{day}", now: date)
end
In rails you can do it this way
Date.today.next_week(day = :monday)

Resources