When using Rails, we often need to run tasks in the background without affecting website performance. Rails 4.2 introduced the ActiveJob framework to standardize scheduling background jobs.
A simple example where the ActiveJob use-case fits correctly is sending an email verification when a user signs up as shown below.
In the above example, we have passed user_id
and email_verification_code
as arguments
to the perform
method.
ActiveJob performs serialization and deserialization on these arguments.
The below argument types are supported (before Rails 7) by default:
Before
As seen in the above list Range
as an argument was not supported before Rails 7.
Let’s take an example where for a given date range we need to pass log report to admins. If we try to pass the date range as an argument, it raises an error as shown below:
In Rails 6 or above, we can resolve the above issue by adding a custom serializer for the Range argument type. To know more about custom serializers, please refer to our previous blog.
After
With
the changes
in Rails 7, we can now pass range
as an argument type and avoid adding custom serializers.
The above AdminReportJob
will work without raising any error.