Created
December 11, 2015 07:52
-
-
Save Teino1978-Corp/98bcb73a378db1656db8 to your computer and use it in GitHub Desktop.
Bulk update example in ActiveRecord Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new_values = { 10 => "test1", 20 => "test2" } | |
value = new_values.map { |product_id, value| "(#{product_id}, #{value})" }.join(",") | |
ActiveRecord::Base.connection.execute(%( | |
UPDATE products AS p SET value = v.value | |
FROM (values #{values}) AS v(id, value) | |
WHERE p.id = v.id | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment