forked from zdennis/activerecord-import
-
Notifications
You must be signed in to change notification settings - Fork 0
Examples
jeffw-wherethebitsroam edited this page Jan 28, 2012
·
6 revisions
For information about requiring activerecord-import in your project please see Requiring.
The #import method can take an array of column names (string or symbols) and an array of arrays. Each child array represents an individual record and its list of values in the same order as the columns. This is the fastest import mechanism and also the most primitive.
columns = [ :title, :author ] values = [ ['Book1', 'FooManChu'], ['Book2', 'Bob Jones'] ] # Importing without model validations Book.import columns, values, :validate => false # Import with model validations Book.import columns, values, :validate => true # when not specified :validate defaults to true Book.import columns, values
The #import method can take an array of models. The attributes will be pulled off from each model by looking at the columns available on the model.
books = [ Book.new(:title => "Book 1", :author => "FooManChu"), Book.new(:title => "Book 2", :author => "Bob Jones") ] # without validations Book.import books, :validate => false # with validations Book.import books, :validate => true # when not specified :validate defaults to true Book.import books
The #import method can take an array of column names and an array of models. The column names are used to determine what fields of data should be imported. The following example will only import books with the :title field:
books = [ Book.new(:title => "Book 1", :author => "FooManChu"), Book.new(:title => "Book 2", :author => "Bob Jones") ] columns = [ :title ] # without validations Book.import columns, books, :validate => false # with validations Book.import columns, books, :validate => true # when not specified :validate defaults to true Book.import columns, books # result in table books title | author --------|-------- Book 1 | NULL Book 2 | NULL