carrierwaveuploader/carrierwave · error · CarrierWave::IntegrityError
errors.messages.min_size_error
Error message
errors.messages.min_size_error
What it means
CarrierWave raises CarrierWave::IntegrityError with this message from the `before :cache` callback `check_size!` when the uploaded file is smaller than the minimum of the Range returned by the uploader's `size_range` (values are bytes). The check only runs if `size_range` returns an actual `::Range`; the minimum is formatted for humans with `number_to_human_size` before the error is raised. It fires during caching, i.e. the moment a file is assigned to the mounted attribute.
Source
Thrown at lib/carrierwave/uploader/file_size.rb:34
#
# [NilClass, Range] a size range (in bytes) which are permitted to be uploaded
#
# === Examples
#
# def size_range
# 3256...5748
# end
#
def size_range; end
private
def check_size!(new_file)
size = new_file.size
expected_size_range = size_range
if expected_size_range.is_a?(::Range)
if size < expected_size_range.min
raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.min_size_error", :min_size => ActiveSupport::NumberHelper.number_to_human_size(expected_size_range.min))
elsif size > expected_size_range.max
raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.max_size_error", :max_size => ActiveSupport::NumberHelper.number_to_human_size(expected_size_range.max))
end
end
end
end # FileSize
end # Uploader
end # CarrierWave
View on GitHub (pinned to b5f0abe10e)
Solutions
- Lower the minimum of `size_range` — if you only want a maximum, write `0..5.megabytes` (or `nil..max` is not valid; use 0 as min).
- Confirm the actual byte size being rejected (`file.size`, or the browser's File.size) against the range; remember values are bytes, not kilobytes.
- If a real floor is intended, communicate it in the UI (accept/min-size hints) and validate client-side before the POST.
- Rescue `CarrierWave::IntegrityError` where the attribute is assigned and convert the message into a model/user-facing validation message.
Example fix
# before
class AvatarUploader < CarrierWave::Uploader::Base
def size_range
1.megabyte..5.megabytes
end
end
# uploading a 300 KB avatar => CarrierWave::IntegrityError: min_size_error
# after (only a maximum was intended)
class AvatarUploader < CarrierWave::Uploader::Base
def size_range
0..5.megabytes
end
end Defensive patterns
Strategy: validation
Validate before calling
min = 1.megabyte # keep in sync with the uploader's size_range if uploaded_io.respond_to?(:size) && uploaded_io.size < min errors.add(:avatar, :too_small, count: ActiveSupport::NumberHelper.number_to_human_size(min)) end
Try / catch
begin user.avatar = params[:avatar] rescue CarrierWave::IntegrityError => e # e.message is min_size_error with the humanized minimum errors.add(:avatar, :file_too_small, message: e.message) end
Prevention
- If you only mean to cap the maximum, write size_range starting at 0 (0..5.megabytes).
- Store the min/max in constants referenced by both the uploader and client-side checks (File.size in JS).
- Prefer allowing small files and enforcing minimum dimensions/quality at image processing time instead of a byte floor.
- Unit-test size_range boundaries: min-1, min, max-1, max (and remember Ruby's ... exclusive end).
When it happens
Trigger: Defining `def size_range; 1.megabyte..5.megabytes; end` in the uploader and assigning a file of, say, 300 KB (`new_file.size < range.min`). Typical with minimum-quality rules (e.g. avatar must be at least 1 MB) or ranges written as an exact size rather than an interval.
Common situations: `size_range` set to enforce a maximum only (e.g. `5.megabytes..10.megabytes`) when the intent was `0..5.megabytes`, so every small file is rejected; tiny auto-generated files (1x1 pixel placeholders, empty PDFs) failing an aggressive minimum after a policy change; teams surprised that a 'max size' upload rule also rejects small files because the Range has a non-zero min.
Related errors
- errors.messages.max_size_error
- errors.messages.extension_allowlist_error
- errors.messages.extension_denylist_error
- Version #{version} doesn't exist!
- could not download file: #{e.message}
AI-assisted analysis of carrierwaveuploader/carrierwave@b5f0abe10e (2026-08-21).
Data as JSON: /api/errors/e12a3c6f0d860abe.
Report an issue: GitHub.