Last active
January 3, 2016 19:49
-
-
Save iamjohnlong/8511379 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I have a hash `dates` that I get from "DateTime.now - 7, DateTime.now" in my database. | |
# I need to turn it into a full week where if the date doesn't exist put a 0 in as the value of the date key. | |
dates = {"2014-01-15"=>1, "2014-01-17"=>1, "2014-01-18"=>1, "2014-01-19"=>17} | |
week_hash = { | |
"2014-01-13"=>0, | |
"2014-01-14"=>0, | |
"2014-01-15"=>1, | |
"2014-01-16"=>0, | |
"2014-01-17"=>1, | |
"2014-01-18"=>1, | |
"2014-01-19"=>17 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks good! Yeah, needing to serialize that data into a JSON object is definitely a different story. One quick thing: you're generating two equivalent dates two different ways at two different times. If your database connection stalls for a few seconds, you could accidentally end up with mismatched results. Just generate your date once, like
so you don't end up with time mismatches. The deprecation warnings are because that's the old Rails 3.x syntax for
.count
. Look into the.group
and.order
methods instead.