Rails usage of after_commit vs after_save

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.

»
Author's profile picture Akshay Mohite on Rails |

Ruby Detect vs Select method on Enumerable

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.

»
Author's profile picture Akshay Mohite on Rails |

Rails generate password protected PDF file with Prawn

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.

»
Author's profile picture Akshay Mohite on Rails |

Rails building json API responses with JBuilder

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.

»
Author's profile picture Akshay Mohite on Rails |

Rails adds before? after? methods on date time classes

Rails Date, DateTime, Time, TimeWithZone class objects could be compared using less than, greater than signs. Rails added methods before? and after? to compare objects of these datetime classes.

»
Author's profile picture Akshay Mohite on Rails |

Rails uses did_you_mean spell checker ruby gem for suggestions

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.

»
Author's profile picture Akshay Mohite on Ruby |

Ruby Chronic gem - parse datetime in natural language

To parse natural language datetime string in Ruby, we can use gem Chronic. Chronic is a natural language date parser in Ruby. This comes very handy, if we want to parse natural language datetime input from user to Time / DateTime classes.

»
Author's profile picture Akshay Mohite on Ruby |

Rails Range cover? includes? methods support Range argument

Rails CompareWithRange core extension adds support of Range as an argument to Range#===, Range#cover? and Range#includes? methods. 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 old active_support/core_ext/range/include_range with Rails 6.1.

»
Author's profile picture Akshay Mohite on Rails |

Ruby 2.6 adds Random.bytes method

Ruby 2.6.0-preview2 has been released recently. Random.bytes method was introduced with the release. This is equivalent to Random::DEFAULT.bytes. The feature returns random bytes based on the length argument passed to it.

»
Author's profile picture Akshay Mohite on Ruby |

Rails skip_before_action callback ignores conditional

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.

»
Author's profile picture Akshay Mohite on Rails |

Rails parse date time string in UTC zone

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.

»
Author's profile picture Akshay Mohite on Rails |

Rails ActiveSupport adds Enumerable#index_with method

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.

»
Author's profile picture Akshay Mohite on Rails |

Rails set timezone per request

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.

»
Author's profile picture Akshay Mohite on Rails |

Rails render 404 not found from a controller action

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.

»
Author's profile picture Akshay Mohite on Rails |

Do not set Time.zone to avoid timezone issues in Rails

Setting Time.zone in a Rails application changes time zone globally. Whenever 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 Time.zone. Instead, use in_time_zone method to get time in desired zone.

»
Author's profile picture Akshay Mohite on Rails |