All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What does the error 'is out of range' mean in Ruby on Rails? Range Error - Integer with a limit of 4 bytes

Post cover: What does the error 'is out of range' mean in Ruby on Rails? Range Error - Integer with a limit of 4 bytes
This content has been automatically translated from Ukrainian.
RangeError can be seen in Ruby on Rails after trying to write a number that is too large (or too small) to the database. The error may look like this:
999999999999999999999999999999999999999 is out of range for ActiveModel::Type::Integer with limit 4 bytes
or 
999999999999999999999999999999999999999 is out of range for ActiveRecord::Type::Integer with limit 4
This means that we are trying to shove something into the database that is outside the allowed range.
For numbers in the database, we can use the data types int and bigint. And each of these types has its own limitations. I have already written about this in the post about the difference between int and bigint.
I will duplicate it here as well:
  • Int can have values between -2147483648 and 2147483647.
  • Bigint between -9223372036854775808 and 9223372036854775807.
So these are architecture-fixed limits for int (32-bit number) and bigint (64-bit number).
The error can be resolved by using numbers within these ranges. Sometimes this error occurs when testing inputs on the frontend. QA enters a large number into a field and we get a RangeError. This is usually fixed by adding validations and limits (both on the fe and be sides).
But what to do if this is a model id and we need a larger range of possible values? There are actually several options.
  • Switching from ID to UUID (Universally Unique Identifier). There can be quite a few variations of implementation. It depends on the stack and scope.
  • Sharding the database into clusters. There are also many variations of implementations here. But conditionally - each cluster (shard) will have a prefix and its own range of bigint.
  • In large systems, you can see composite keys that are created from several different columns. But this is also a specific option, the implementation of which depends on many factors.

This post doesn't have any additions from the author yet.

What is the difference between immediate value and reference in Ruby?
29 May 12:00

What is the difference between immediate value and reference in Ruby?

meme code
meme code@memecode
Why does Ruby code return nil after executing puts?
29 May 20:30

Why does Ruby code return nil after executing puts?

meme code
meme code@memecode
What is the difference between nil and false in Ruby?
29 May 20:59

What is the difference between nil and false in Ruby?

meme code
meme code@memecode
Why is an empty string in Ruby not false?
31 May 14:39

Why is an empty string in Ruby not false?

meme code
meme code@memecode
The scope of a local variable in Ruby
03 Jun 16:46

The scope of a local variable in Ruby

meme code
meme code@memecode
What is the difference between int and bigint in Ruby? Minimum and maximum values.
13 Jun 06:37

What is the difference between int and bigint in Ruby? Minimum and maximum values.

meme code
meme code@memecode
What is immutability and mutability?
19 Jun 07:48

What is immutability and mutability?

meme code
meme code@memecode
What will be the result of adding 10.5 and 10?
23 Jun 13:23

What will be the result of adding 10.5 and 10?

meme code
meme code@memecode
[Ruby] What is the difference between variables that start with @, @@, and $?
23 Jun 14:00

[Ruby] What is the difference between variables that start with @, @@, and $?

meme code
meme code@memecode
What is a function in programming?
24 Jun 18:15

What is a function in programming?

meme code
meme code@memecode
[Fix] extconf.rb failed during the installation of the Ruby library Gosu
27 Jun 16:38

[Fix] extconf.rb failed during the installation of the Ruby library Gosu

meme code
meme code@memecode
How to make an empty git commit?
28 Jun 08:33

How to make an empty git commit?

meme code
meme code@memecode