Created
          July 6, 2011 13:57 
        
      - 
      
 - 
        
Save superp/1067281 to your computer and use it in GitHub Desktop.  
    Group by example in MongoMapper
  
        
  
    
      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
    
  
  
    
  | class Tweet | |
| include MongoMapper::Document | |
| key :id_str, String | |
| key :from_user_id, Integer | |
| belongs_to :track_item | |
| def self.grouped_by(column, options = {}) | |
| map_function = "function() { emit( this.#{column}, 1); }" | |
| # put your logic here (not needed in my case) | |
| reduce_function = %Q( function(key, values) { | |
| return true; | |
| }) | |
| collection.map_reduce(map_function, reduce_function, options) | |
| end | |
| end | |
| Tweet.grouped_by(:from_user_id, :query => {:track_item_id => track_item.id}).find().count | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
undefined method `last' for nil:NilClass
I get this error when I try and run map reduce, any idea why?