Coder Social home page Coder Social logo

batch_insert's Introduction

Batch Insert

This plugin adds batch insertion capabilities to ActiveRecord model classes.

The API is very similar to the standard new and create functions provided by ActiveRecord, but will not return a saved object to the caller.

License

This plugin is released under the MIT license, and was contributed to the Rails community by the good people at Software Projects

Example

Previously:

100.times { ModelClass.create! :name => 'Test', :description => 'An example object being created.' }

Now:

ModelClass.batch_insert do
	100.times { ModelClass.insert :name => 'Test', :description => 'An example object being created.' }
end

Batch insertions return the same result as new()

ModelClass.batch_insert do
	args = {:name => 'Test', :description => 'An example object being created.'}

	ModelClass.insert(args)					# ModelClass instance
	ModelClass.insert(args).new_record?		# true
	ModelClass.insert(args).id				# nil
end		# The insertion is done at this point in the code.

Invalid objects will immediately cause an exception

ModelClass.batch_insert do
	invalid_args = {}
	ModelClass.insert(invalid_args)			# Exception!
end

Constraint violations will cause an exception when the batch is inserted

ModelClass.batch_insert do
	conflict = {:unique_value => 'Collide!'}
	ModelClass.insert(conflict)
	ModelClass.insert(conflict)
end		# Exception!

Custom batch size is supported (the default is unlimited)

ModelClass.batch_insert :batch_size => 10 do
	# This loop will cause 3 INSERT statements
	37.times do |i|
		ModelClass.insert :name => "object #{i}", :description => "The #{i.ordinalize} object"
	end
end     # And the last 7 objects will be inserted here.

batch_insert's People

Contributors

smangelsdorf avatar

Watchers

John Jeffery avatar James Cloos avatar  avatar Anthony Marendy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.