Hi,
My results are getting out of order after I do the following command:
count = result.inject({}) { |hsh, row| hsh[row[‘name’]] =
row[‘count’].to_i;
hsh }
Why?
Here are more specifics:
My complete method is this (based off of acts_as_taggable code - not’
DHHs,
but the original one):
def self.sql_to_count_plays(lookback)
sql = [“SELECT t.name as name, COUNT(*) as count
FROM histories h, tags t
WHERE h.tag_id = t.id
AND created_on > ?
GROUP BY tag_id
ORDER BY name”, lookback]
result = find_by_sql(sql)
count = result.inject({}) { |hsh, row| hsh[row[‘name’]] =
row[‘count’].to_i; hsh }
count
end
The “result” is in the proper alpha order:
=> [#<Tag:0x3606ab0 @attributes={“name”=>“baseball”, “count”=>“2”}>,
#<Tag:0x360
6a68 @attributes={“name”=>“name”, “count”=>“29”}>, #<Tag:0x3606900
@attributes={
“name”=>“new”, “count”=>“3”}>, #<Tag:0x36065b8 @attributes={“name”=>“new
test fo
r tags”, “count”=>“4”}>, #<Tag:0x3606210 @attributes={“name”=>“serial”,
“count”=
“4”}>, #<Tag:0x3605fd0 @attributes={“name”=>“test”, “count”=>“1”}>]
but once it is processed through the result.inject function it gets out
of
alpha order:
=> {“new”=>3, “name”=>29, “new test for tags”=>4, “serial”=>4,
“test”=>1, "
baseball"=>2}
I can’t figure out why or how to correct it. I don’t have the latest
pickaxe
book which documents the inject function and can’t find much
documentation
on it on the web. I could turn it into an sorted array using “count =
count.sort_by { |r| r.name }”, but I don’t want it in an array. In need
it
in a hash.
Any ideas?
Kind regards,
Steve O.
http://www.smarkets.net