I wanted to group the total checkin in a day using the groupdate gem with this code.
from = Time.zone.now.beginning_of_day
to = Time.zone.now.end_of_day
customer_checkins = CustomerCheckin.where(created_at: from..to, account_id: 139)
hours = customer_checkins.group_by_hour_of_day(:created_at).count
The result is weird because all counts are tagged into hour 0 instead of being tagged to its specific hour of day. The result is shown below.
{0=>677, 1=>0, 2=>0, 3=>0, 4=>0, 5=>0, 6=>0, 7=>0, 8=>0,
9=>4, 10=>0, 11=>0, 12=>0, 13=>0, 14=>0, 15=>0, 16=>0, 17=>0,
18=>0, 19=>0, 20=>0, 21=>0, 22=>0, 23=>0}
What could be the problem with this one?
You can run your code in rails c.The console will pop up your sql query,so it may help you to find out the problem.
I guess group_by_hour_of_day is your CustomerCheckin model function.I don't now whether this function has trim your :created_at format. By default the :created_at will contain the second like "%Y-%m-%d %H:%M:%S",so the sql will group by base on the second not the hour. You may try to trim the :created_at format. Like
DateTime.parse("2015-05-08 02:30:01 +0800").strftime("%Y-%m-%d %H")
=> "2015-05-08 02"
Hope it will help you.
Related
I'm attempting to convert a string date into a date that can be stored in the database. These are my attempts:
params[:event][:start_date] = '03-21-2016'
DateTime.strptime(params[:event][:start_date], '%Y-%m-%dT%H:%M:%S%z')
or
DateTime.strptime(params[:event][:start_date], '%m-%d-%Y')
However I keep getting an invalid date error. I'm not sure what I'm doing wrong.
One way to do it would be this:
date_string = '03-21-2016'
month, day, year = date_string.split('-').map(&:to_i)
DateTime.new(year, month, day)
You need to understand what each fragment (eg %Y) in the format string ('%Y-%m-%dT%H:%M:%S%z') means: read this.
http://apidock.com/rails/ActiveSupport/TimeWithZone/strftime
Once you know this, you can tailor a format string to the date string you have, or expect to get: in this case, "%m-%d-%Y".
When debugging create a new, basic and simple, version of the code and test that.
require 'date'
params = '03-21-2016'
DateTime.strptime(params, '%m-%d-%Y') # => #<DateTime: 2016-03-21T00:00:00+00:00 ((2457469j,0s,0n),+0s,2299161j)>
Note the order for the format: '%m-%d-%Y', which works. That's the problem with your first attempt, where you tried to use%Y-%m-%d. There is NO month21or day2016`.
Your second attempt is valid but your question makes it appear it doesn't work. You need to be more careful with your testing:
params = {event:{start_date:'03-21-2016'}}
DateTime.strptime(params[:event][:start_date], '%m-%d-%Y') # => #<DateTime: 2016-03-21T00:00:00+00:00 ((2457469j,0s,0n),+0s,2299161j)>
I'm looking for a way of getting a grouped output for every minute records are being made. I'm looking for a result that would look something like this:
{"2015-01-05 12:05" => 5, "2015-01-05 12:06" => 2}
When using DATE_PART or EXTRACT I get this as result
{0.0=>1, 48.0=>2, 23.0=>3, 42.0=>2, 47.0=>2, 54.0=>1, 46.0=>2, 2.0=>1, 44.0=>2, 45.0=>2, 53.0=>1, 6.0=>3, 49.0=>1, 56.0=>2, 22.0=>1, 51.0=>1, 43.0=>2, 24.0=>2, 5.0=>1, 7.0=>1, 37.0=>2, 33.0=>2, 50.0=>1, 1.0=>2}
This is my code:
Model.group("DATE_PART('minute', created_at)").count
Model.group("EXTRACT(MINUTE FROM created_at)").count
Model.group("to_char(created_at,'YYYY-MM-DD HH24:MI')").count
I'm using a gem called Desk to return data from desk.com's API. When I run Desk.customers(:custom_external_id => temp.id) it returns a lovely set of data.
I'm looking for the integer 71095620 from id=71095620.
=> #<Hashie::Rash count=20 page=1 results=[#<Hashie::Rash customer=#<Hashie::Rash addresses=[] custom_external_id="58749" emails=[#<Hashie::Rash email=#<Hashie::Rash created_at="2013-02-13T15:59:26-08:00" email="CENSORED" i
d=33622514 updated_at="2013-02-13T15:59:26-08:00" verified_at=nil>>] first_name="CENSORED" id=68712186 language=nil last_name="CENSORED" phones=[#<Hashie::Rash phone=#<Hashie::Rash created_at="2013-02-13T16:00:45-08:00" id=1301079 phone="CENSORED" updated_at="2013-02-13T16:00:45-08:00">>] twitters=[nil]>>, #<Hashie::Rash customer=#<Hashie::Rash addresses=[] custom_external_id="58749" emails=[] first_name="CENSORED" id=71095620 language=nil last_name="CENSORED" phones=[] twitters=[nil]>>] total=2>
There are obviously multiple id fields all over this and i'm not sure which way is best to extract that integer. Thanks in advance!
According to the Hasie::Rash readme, You can probably do this
response = Desk.customers(:custom_external_id => temp.id)
response.results[1].customer.id
I need to update time info in a DateTime.
I get a string in the format "14" or "14:30" (for example), so I need to give it to Time parser to get the right hour. Then I need to update self.start_at which is a datetime which already has a time, but I need to update it.
self.start_at_hours = Time.parse(self.start_at_hours) # example 14:30:00
# NEED TO UPDATE self.start_at which is a datetime
I was using the change method on self.start_at but it only takes hour and minutes separated and I'm not sure what should I do.
Have you thought about doing somethings like this?
time_to_merge = Time.new
date_to_merge = Date.today
merged_datetime = DateTime.new(date_to_merge.year, date_to_merge.month,
date_to_merge.day, time_to_merge.hour,
time_to_merge.min, time_to_merge.sec)
For replacing time, the .change() method should work, like this:
my_datetime = my_datetime.change(hour: my_time.hour, min: my_time.min, sec: my_time.sec)
For adding time, try converting to seconds and then adding them:
my_datetime += my_time.seconds_since_midnight.seconds
With rails, I can format a number into a US phone number with number_to_phone
I'am using using european phone format, which means that I want to group numbers with the following format, n is a variable:
(n*x) xxx-xxx
Some examples
6365555796 => (6365) 555-796
665555796 => (665) 555-796
How to achieve that with the latest rails 3.0.7?
how about so:
def to_phone(num)
groups = num.to_s.scan(/(.*)(\d{3})(\d{3})/).flatten
"(#{groups.shift}) #{groups.shift}-#{groups.shift}"
end
irb(main):053:0> to_phone 318273612
=> "(318) 273-612"
irb(main):054:0> to_phone 3182736122
=> "(3182) 736-122"
irb(main):055:0> to_phone 31827361221
=> "(31827) 361-221"
...
i think you have to write your own method for this there is no built-in method in my knowledge for it and i think you can achieved the desired thing by writing apropriate regular expression for that.
have you tried validate_format_for with: there u can write specific regualr expression for it [see this] http://ruby-forum.com/topic/180192 for writing your own helper for it
check this gem here is what you need http://github.com/floere/phony
A faster approach:
def to_european_phone_format(number)
"(#{number/10**6}) #{number/10**3%10**3}-#{number%10**3}"
end