Reputation: 12653
I have an AR query that returns a hash of events per month, ordered by month
o.events.group("to_char(date,'MM')").order("to_char(date,'MM')").size()
I'm using numeric months in this query as it was the best way I could find to get things in the correct order, and I also need to do some other manipulations on the hash.
Before display the results, I need to convert the numeric months back to words. I added the following to the end of the query
.each_key{ |key| Date::MONTHNAMES[key] }
But i get
TypeError: can't convert String into Integer.
So i tried
.each_key{ |key| Date::MONTHNAMES[key.to_i] }
But the months remain in numeric form
{"01"=>4, "02"=>3.....
How can i manipulate this hash to get
{"January"=>4, "February"=>3.....
Upvotes: 0
Views: 238
Reputation: 87416
There are ways to generate a new hash but I think you could just convert the strings to month names in your view right before displaying them. Use the code you already wrote inside the block in your question but put it in your view.
Upvotes: 0
Reputation: 27114
This ? :
def number_to_month(number)
(Time.now.beginning_of_year + number.months).strftime("%B")
end
Upvotes: 0
Reputation: 198324
Make a new Hash. If you can't, make a new key in the current hash and delete the original key. You can't simply change a key, since key
is a local variable in the block, and changing it in no way impacts the contents of the Hash.
Upvotes: 2