Skip to content

Instantly share code, notes, and snippets.

@drakmail
Created December 11, 2015 07:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drakmail/7b337827907ccfcec0af to your computer and use it in GitHub Desktop.
Save drakmail/7b337827907ccfcec0af to your computer and use it in GitHub Desktop.
Bulk update example in ActiveRecord Rails
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
))
@onefriendaday
Copy link

Great code! But I think the variable on line 2 should be "values" and not "value".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment