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)