ruby
53 lines · 8 steps
Bulk CSV imports with upsert_all in Rails
A Rails API endpoint parses an uploaded CSV and bulk-upserts products in a single query, keyed by SKU.
Explained by
highlit
1require "csv"
2
3class Api::V1::ProductImportsController < Api::V1::BaseController
4 MAX_ROWS = 10_000
5
6 def create
7 csv_file = params.require(:file)
8 rows = parse_csv(csv_file)
9
10 if rows.empty?
11 return render json: { error: "No valid rows found" }, status: :unprocessable_entity
12 end
13
14 if rows.size > MAX_ROWS
15 return render json: { error: "Too many rows (max #{MAX_ROWS})" }, status: :unprocessable_entity
16 end
17
18 result = Product.upsert_all(
19 rows,
20 unique_by: :sku,
21 update_only: %i[name price_cents stock currency],
22 returning: %w[id sku]
23 )
24
25 render json: {
26 imported: result.length,
27 skus: result.rows.flatten
28 }, status: :created
29 rescue CSV::MalformedCSVError => e
30 render json: { error: "Malformed CSV: #{e.message}" }, status: :unprocessable_entity
31 end
32
33 private
34
35 def parse_csv(file)
36 now = Time.current
37
38 CSV.foreach(file.path, headers: true, header_converters: :symbol).filter_map do |row|
39 sku = row[:sku]&.strip
40 next if sku.blank?
41
42 {
43 sku: sku,
44 name: row[:name]&.strip,
45 price_cents: (row[:price].to_f * 100).round,
46 stock: row[:stock].to_i,
47 currency: row[:currency].presence || "USD",
48 created_at: now,
49 updated_at: now
50 }
51 end
52 end
53end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1upsert_all writes many rows in one query, sidestepping model callbacks and per-row round trips.
- 2Guard bulk operations with size and emptiness checks before touching the database.
- 3Normalizing and filtering rows during parsing keeps invalid data out of the upsert entirely.
Related explainers
ruby
class Order class InvalidTransition < StandardError; end TRANSITIONS = {
A state machine for order transitions in Ruby
state-machine
data-driven
error-handling
Intermediate
8 steps
ruby
class CreateOrderItems < ActiveRecord::Migration[7.1] def change create_table :order_items do |t| t.references :order, null: false, foreign_key: { on_delete: :cascade }
Enforcing order-item integrity in Rails
migrations
foreign-keys
validations
Intermediate
7 steps
ruby
class Registration < ApplicationRecord belongs_to :event validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
Validating registrations in Rails
validations
i18n
error-handling
Intermediate
8 steps
ruby
class ApacheLogParser LINE_PATTERN = /\A(?<ip>\S+)\s\S+\s\S+\s\[(?<time>[^\]]+)\]\s"(?<method>[A-Z]+)\s(?<path>\S+)\s(?<protocol>[^"]+)"\s(?<status>\d{3})\s(?<bytes>\d+|-)/ TIME_FORMAT = "%d/%b/%Y:%H:%M:%S %z"
Parsing Apache logs with named captures
regex
named-captures
parsing
Intermediate
6 steps
ruby
require "csv" def parse_transaction_row(line) fields = CSV.parse_line(line, headers: false, skip_blanks: true)
Parsing one CSV transaction row in Ruby
parsing
type-coercion
error-handling
Intermediate
6 steps
ruby
class NewCommentNotifier < ApplicationNotifier deliver_by :database deliver_by :email do |config|
Multi-channel notifications with Noticed in Rails
notifications
delivery-methods
fan-out
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/bulk-csv-imports-with-upsert_all-in-rails-explained-ruby-ca3b/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.