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
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
ruby
class LogAggregator BUCKET_FORMAT = "%Y-%m-%dT%H:%M" def initialize(entries)
Bucketing log entries by the minute in Ruby
aggregation
hashing
enumerable
Intermediate
5 steps
ruby
class WeeklySignupsReport DEFAULT_WEEKS = 12 def initialize(weeks: DEFAULT_WEEKS, source: User.all)
Building a weekly signups report in Rails
service object
aggregation
group by
Intermediate
7 steps
ruby
class ApplicationController < ActionController::Base EXPERIMENTS = { checkout_button_color: %w[control blue green], onboarding_flow: %w[control streamlined]
How A/B test cohorts are assigned in Rails
a-b-testing
cookies
hashing
Intermediate
8 steps
ruby
class SnippetHighlighter CONTEXT_RADIUS = 60 MAX_TERMS = 8
Building search-result snippets in Ruby
regular-expressions
text-processing
search
Intermediate
9 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.