Created
December 11, 2015 07:37
-
-
Save drakmail/7b337827907ccfcec0af 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
Great code! But I think the variable on line 2 should be "values" and not "value".