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 |

Rails 5.1 Encrypted secrets management

Before Rails 5.1, secrets management was a hassle. Gems like dotenv-rails, sekrets were being used to manage secrets in Rails applications. Rails 5.1 released secrets management feature based on sekrets gem. This article will explain how to manage application secrets with Rails 5.1 applications.

»
Author's profile picture Akshay Mohite on Rails |

Rails ActiveRecord Subqueries

Writing subqueries can be a tedious job, when working with Rails ActiveRecord. Rails ActiveRecord can handle subqueries. Subqueries can be written by passing ActiveRecord Relation in where clause. This article discusses how to write subqueries with ActiveRecord in Rails.

»
Author's profile picture Akshay Mohite on Rails |

Rails find missing indexes on tables with lol_dba gem

Missing indexes on database tables causes performance issues. lol_dba gem helps finding out missing indexes on database table in Rails code. lol_dba performs static analysis of the code to find out missing indexes. We will take a look at how to use lol_dba to find out indexes. lol_dba also helps creating a migration to add the requred indices.

»
Author's profile picture Akshay Mohite on Rails |