Reputation: 149
Suppose that I have a hash of hashes as following. One can use "update" to update all the records as follows.
people = { 1 => { "first_name" => "David", "last_name" => "Freeman" }, 2 => { "first_name" => "Jeremy", "last_name" => "Stone" } .....}
Person.update(people.keys, people.values)
This will run an UPDATE query for each record. How can I use "update_all" to achieve this which runs a single query for all the records?
Referred to (this) but couldn't find the solution.
Thanks in advance.
Upvotes: 3
Views: 1627
Reputation: 1343
I don't think this is possible. This is not the nature of "update_all". Imagine writing a SQL query for your required result, it will involve matching each id and then doing the required update for each id, hence contrasting it from what "update_all" does.
You should use "update_all" when performing same update over a set of tuples, but obviously in your case first_name and last_name are different for each tuple.
I think using just "update" will not affect performance much, considering the id's are indexed. However, you always have the option of writing native SQL statements.
Upvotes: 2