Last active
September 24, 2015 22:22
-
-
Save RafaelMCarvalho/6add6bc57d13c066802d to your computer and use it in GitHub Desktop.
A concern that adds a perfomatic .bulk_update method for ActiveRecord classes.
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
module BulkUpdatable | |
extend ActiveSupport::Concern | |
self.included do |base| | |
# Usage: | |
# | |
# User.bulk_update([{id: 1, name: 'Foo'}, {id: 2, name: 'Bar'}]) | |
# (1.5ms) UPDATE "users" SET "id" = 1, "name" = 'Foo' WHERE "users"."id" = 1; | |
# UPDATE "users" SET "id" = 2, "name" = 'Bar' WHERE "users"."id" = 2 | |
# => #<PG::Result:0x007f814aa04c40 status=PGRES_COMMAND_OK ntuples=0 nfields=0 cmd_tuples=1> | |
# | |
def self.bulk_update(attributes_list) | |
table = Arel::Table.new(self.table_name) | |
sql_sets = [] | |
attributes_list.each do |attributes| | |
update_manager = Arel::UpdateManager.new table.engine | |
sets = attributes.map { |k, v| [table[k], v] } | |
update_manager.set(sets).where(table[:id].eq(attributes[:id])).table(table) | |
sql_sets << update_manager.to_sql | |
end | |
ActiveRecord::Base.connection.execute(sql_sets.join(";\n")) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Benchmark of 1000 entries updates: