Rails provides ActiveRecord callbacks to perform an action after a record is updated in the database. But, when to use after_commit vs after_save really depends on the activities that need to be done in these callbacks.»
There is a subtle difference between Detect and Select method in Ruby. Select and detect both operate on Enumerable and require a condition to return the result. The primary difference between the two is as given below.»
Generating password protected PDF file is required if data being exported is sensitive. In Rails, Prawn gem can be used to generate password protected PDF file.»
Rails is a MVC web application framework which supports building complex websites without writing any APIs. But, client side libraries are evolving at brisk pace with wide-spread use of React JS, AngularJS, VuJS etc. Thus, building API endpoints is a very important tasks these days.»
Rails Date, DateTime, Time, TimeWithZone class objects
could be compared using less than,
greater than signs.
to compare objects
of these datetime classes.
When coding in Ruby, we tend to make typo errors. This can take up time if not quickly identified. did_you_mean spellchecker gem helps detect such errors and also presents suggestions that can resolve typo errors.»
To parse natural language datetime string in Ruby,
we can use gem
Chronic is a
date parser in Ruby.
This comes very handy,
if we want to parse natural language datetime input
from user to
CompareWithRange core extension adds support
Range as an argument
Ruby already has support for these three methods,
but ruby does not support Range value as an argument.
This change is targetted to release and deprecate
with Rails 6.1.
has been released recently.
method was introduced with the release.
This is equivalent to
The feature returns random bytes
the length argument passed to it.
Before action callback in Rails has support to add conditions like if and unless. The callback is executed only if the conditions evaluate to true. But, skip_before_action callback does not support conditionals. In this article we will learn how to skip certain action based on some condition.»
Sometimes we need to parse a date string in UTC timezone in Ruby. Time.parse considers system time zone whereas we might want to parse time in UTC or some other custom timzone. This article helps parse time in UTC or any other zone.»
Rails ActiveSupport Enumerable adds index_with method. This method allows creating hash with a value from block passed or default value. This is useful when we want to create a hash and value needs to be generated with some custom logic.»
Sometimes we need to have a request be processed in a particular timezone in Rails. To set a timezone per request in Rails, we can have an around filter that sets timezone, runs the requests and then resets timezone to what it was before the request.»
Rails redirects route not found requests to 404.html page. Sometimes, we need to redirect to 404 not found page from a controller action. This can be done by rendering 404.html page or raising ActionController::RoutingError error.»
Time.zone in a Rails application changes time zone globally.
Time.now is used in any other request,
it uses whatever Time.zone is set to earlier.
To avoid Time.zone issues, do not set
in_time_zone method to get time in desired zone.