Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Batch update in ROR

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 192
    Comment on it

    There are always a requirement when you have to update the multiple records at the same time using ActiveRecord with a single HTTP request. All you have to do is create a hash with keys as the primary key and value as another hash of all the fields with their values and call it with the model name as below:

    Model.update

    Lets take an example of this:

    # Updating multiple records:
      user = { 1 => { "first_name" => "User1" }, 2 => { "first_name" => "User2" } }
      User.update(user.keys, user.values)
    

    Here it will call the User model update method with first parameter as the keys and second as the hash of DB fields to update with their fields.

 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: