I have a bunch of posts that have category tags. I am trying to figure out how many times each category has been used.
I use rails with mongodb, BUT I don't think I need to get the appearance of categories from db, so the mongo part doesn't matter.
This is what I still have
@recent_posts = current_user.recent_posts #returns the 10 most recent posts
@categories_hash = {'tech' => 0, 'world' => 0, 'entertainment' => 0, 'sports' => 0}
@recent_posts do | cat |
cat.categories.each do | addCat |
@ categories_hash.increment (addCat) #obviously this is where I'm having problems
end
end
end
message structure
{"_id": ObjectId ("idnumber"), "created_at": "Tue Aug 03 ...", "categories": ["world", "sports"], "message": "the text of the post" , "poster_id": ObjectId ("idOfUserPoster"), "voters": []}I am open to suggestions on how else to get the category count, but in the end I would like to get the number of voters, so it seems to me that the best way is to increase the number of hash_ categories and then add the .length voters, but one thing at a time, I just I'm trying to figure out how to increase the values ββin the hash.
source
share