ruby - increment value in a hash -
I have several posts that have category tags. I am trying to figure out how many times each category has been used.
I am using rails with mosaics, but I do not think that I am getting the categories of categories from DB, so the Mongo part should not make any difference.
This is what I have done so far
@recent_posts = current_user.recent_posts # 10 Returns the most recent posts @categories_hash = {'tech' => 0, 'World' => 0, 'entertainment' => 0, 'game' => 0} @recent_posts | Cat | Do Cat.categories.each | AddCat | @Category_ass.Incert (adkat) # Obviously this is where I am ending the problems
The structure of the post
{"_ id": ObjectId ("idnumber" ), "Created_at": "Tuesday August 03 ...", "Categories": ["world", "game"], "message": "text of post", "poster_id": object id ("idOfUserPoster") , "Voter": []}
I'm open to suggestions for how to get count of categories, but I would like to finally get voters' count , So it seems to me that the best way is to increase categories_hash, and then add voters. Length, but one thing at a time, I'm just trying to explain how to increase values in the hash.
If you are not familiar with the map / reduce and you do not care about scaling, then Not as beautiful as mapping / reducing, but small sites should be sufficient:
@categories_hash = hash.New (0) current_user.recent_posts.each do | Post | Post. Categories. Category | @ Categories_hash [category] + = 1 end of end
Comments
Post a Comment