Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/985a70527fa11a0123c5 to your computer and use it in GitHub Desktop.
Save JoshCheek/985a70527fa11a0123c5 to your computer and use it in GitHub Desktop.
We got mass insertion working!
# s_arb
require 'active_record'
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Schema.define do
self.verbose = false
create_table :customers do |t|
t.string :first_name
t.string :last_name
t.timestamps
end
create_table :invoices do |t|
t.integer :customer_id
t.integer :merchant_id
t.string :status
t.timestamps
end
end
class Customer < ActiveRecord::Base
has_many :invoices
end
class Invoice < ActiveRecord::Base
belongs_to :customer
end
require 'csv'
def to_sql_ary(values)
elements = values.map { |value| ActiveRecord::Base.connection.quote value }
"(" + elements.join(', ') + ")"
end
def insert_rows(tablename:, colnames:, rows:)
rows_sql = rows.map { |values| " #{to_sql_ary values}" }.join(",\n")
ActiveRecord::Base.connection.execute <<-SQL
INSERT INTO #{tablename} #{to_sql_ary colnames}
VALUES #{rows_sql}
SQL
end
def csvname_for(name)
"/Users/josh/deleteme/sales_engines/sally-justin/data/#{name}.csv"
end
def load_csv(name)
CSV.foreach(csvname_for(name), headers: true, header_converters: :symbol)
.each_slice(200) { |rows|
insert_rows tablename: name,
colnames: rows.first.headers,
rows: rows.map { |row| row.to_h.values }
}
end
load_csv 'customers'
load_csv 'invoices'
Customer.find_by(id: 999) # => #<Customer id: 999, first_name: "Clementina", last_name: "Hudson", created_at: "2012-03-27 14:58:15", updated_at: "2012-03-27 14:58:15">
.invoices # => #<ActiveRecord::Associations::CollectionProxy [#<Invoice id: 4833, customer_id: 999, merchant_id: 59, status: "shipped", created_at: "2012-03-17 19:58:15", updated_at: "2012-03-17 19:58:15">, #<Invoice id: 4834, customer_id: 999, merchant_id: 75, status: "shipped", created_at: "2012-03-19 10:58:15", updated_at: "2012-03-19 10:58:15">, #<Invoice id: 4835, customer_id: 999, merchant_id: 58, ...
.count # => 7
Customer.count # => 1000
Customer.first
# => #<Customer:0x007fca4a0f2e38
# id: 1,
# first_name: "Joey",
# last_name: "Ondricka",
# created_at: 2012-03-27 14:54:09 UTC,
# updated_at: 2012-03-27 14:54:09 UTC>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment