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
    • 188
    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:

    1. results.in_groups_of(100, false) {|res|
    2. res_hash = {}
    3. res.each do |ar|
    4. res_hash[ar['cr_id']] = {'profile_id'=>ar['id']}
    5. end
    6. if res_hash.count > 0
    7. User.update(res_hash.keys, res_hash.values)
    8. end
    9. }

    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
Reset Password
Fill out the form below and reset your password: