Created
July 26, 2019 16:29
-
-
Save BigMcLargeHuge/b65f589c510b5bca1c5f184992af21d0 to your computer and use it in GitHub Desktop.
bigUpdate
This file contains 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
#Looking for the best way to update items in a table in mass below is the best I've come up with but it seems hacky and I've read that update() is depreciated | |
class Table < ActiveRecord::Base | |
... | |
class << self | |
def big_update | |
items = Table.all | |
saves=[] | |
items.each do |item| | |
item.value = rand(10) | |
saves << item.as_json | |
end | |
saves = Hash[*saves.map{ |p| [p["id"], p] }.flatten] | |
self.update(saves.keys, saves.values) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment