Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Spliting resultset over array

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 74
    Comment on it

    I remember i was working on a project where there was a large set of data in the resultset and on the basis of that data i have to update some other model but i was unable to do it in batches due to some reason. So i was searching for a method in ruby which can do this thing for me. so i found a method as below:

    in_groups_of(number, fill_with = nil)

    what it use to do is it Splits or iterates over the array in groups of size number. for ex:

    results.in_groups_of(100, false) {|res| 
          res_hash = {}
            res.each do |ar|
              res_hash[ar['cr_id']] = {'profile_id'=>ar['id']}
            end        
            if res_hash.count > 0          
               User.update(res_hash.keys, res_hash.values)
            end
          }
    

    so it was taking the 100 records in res object and than i was iterating it and creating a array and than i was updating the user record at one go. So it was very helpful for me that time.

    Note: if there are less than the no_of_records(100 in case) we specify it will create a single group. and the remaining ones in the second group or so.

    where results = User.find(:all)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: