Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Support for bulk data methods in ActiveRecord (create_many and update_many)
Ruby JavaScript

Merge pull request #9 from madelman-fiksu/id-sequence-fix

This fixes the id sequence problem.
latest commit b1d9d5d64b
@dkhofer dkhofer authored
Failed to load latest commit information.
lib Version bumped to 1.1.3
spec Added specs for new create behavior
.gitignore Only use activerecord in non-dev environments.
.rspec done
.travis.yml Attempting to get travis to work
Gemfile debugger gem
LICENSE change some docs, bump version
README.md change some docs, bump version
Rakefile rspec now supported
bulk_data_methods.gemspec

README.md

bulk_data_methods

MixIn used to extend ActiveRecord::Base classes implementing bulk insert and update operations through {#create_many} and {#update_many}.

Examples

class Company < ActiveRecord::Base
  extend BulkMethodsMixin
end

BULK creation of many rows:

example no options used

rows = [
           { :name => 'Keith', :salary => 1000 },
           { :name => 'Alex', :salary => 2000 }
       ]
Employee.create_many(rows)

example with :returning option to returns key value

rows = [
           { :name => 'Keith', :salary => 1000 },
           { :name => 'Alex', :salary => 2000 }
       ]
options = { :returning => [:id] }
Employee.create_many(rows, options)

example with :slice_size option (will generate two insert queries)

rows = [
           { :name => 'Keith', :salary => 1000 },
           { :name => 'Alex', :salary => 2000 },
           { :name => 'Mark', :salary => 3000 }
     ]
options = { :slice_size => 2 }
Employee.create_many(rows, options)

BULK updates of many rows:

example using "set_array" to add the value of "salary" to the specific employee's salary the default where clause matches IDs so, it works here.

rows = [
           { :id => 1, :salary => 1000 },
           { :id => 10, :salary => 2000 },
           { :id => 23, :salary => 2500 }
     ]
options = { :set_array => '"salary = datatable.salary"' }
Employee.update_many(rows, options)

example using where clause to only update salaries that haven't already been updated (forced exampled).

rows = [
           { :id => 1, :salary => 1000, :company_id => 10 },
           { :id => 10, :salary => 2000, :company_id => 12 },
           { :id => 23, :salary => 2500, :company_id => 5 }
     ]
options = {
     :set_array => '"salary = datatable.salary"',
     :where_datatable => '"#{table_name}.salary <> datatable.salary"'
          }
  Employee.update_many(rows, options)

example setting where clause to the KEY of the hash passed in and the set_array is generated from the VALUES

rows = {
           { :id => 1 } => { :salary => 100000, :company_id => 10 },
           { :id => 10 } => { :salary => 110000, :company_id => 12 },
           { :id => 23 } => { :salary => 90000, :company_id => 5 }
     }
Employee.update_many(rows)
Something went wrong with that request. Please try again.