Tutorials

Easy approach to format Dates in Rails projects

  I saw a lot of projects where the default date format was used. In fact, customizing the date format to provide a human-readable date will be very welcomed from your customers! So what should we do to start making our application awesome? No worries, it’s a very easy process that we will discover together. Example To get started we will…

Continue Reading
Tutorials

ActiveRecord exists? and blank? under the hood.

  When should I use exists? or blank? to check records existence? Does this choice has an impact on our application? Asking those questions is a good sign of curiosity! So we will see together how those methods work under the hood and without a long introduction let’s unpack ActiveRecord and dive deep into the source code. Putting exists? under the…

Continue Reading
Tutorials

Drop a table in production

In this post, we will see an approach how to safely drop a table from a production application without running into weird issues. As an example, we will use a simple blog that allows adding comments to each post and also has a simple stats table where it tracks likes, views, and shares of posts. The model diagram for this example is…

Continue Reading
Tutorials

Rename an ActiveRecord model

Choosing a good name for a class or a variable isn’t easy at all and sometimes there is a requirement to rename existing tables and models to match the updated product audience. So how we should do this migration then: 1- Ask why The first step is to ask why should we rename the model, with a productive mindset and…

Continue Reading
Tutorials

Add DB uniqueness constraint across tables

We saw previously in this post how to add uniqueness database constraint for a single table. But in real-world applications, we have different associations between models, so let’s start with an example: We have a service platform where a user can buy subscriptions for different services, the requirements are the following: A user can have many subscriptions for different services…

Continue Reading
Tutorials

Write unique validation with confidence!

It’s easy to use model validation in Rails application and in this article, we will study some aspects with the uniqueness validator. So let’s start with an easy requirement, we want to have an Account model with a unique email attribute.

This solution works but it’s not enough. For beginners, it’s important to understand how validation work in Rails.…

Continue Reading
Tutorials

Fix missing environment variables for production

It’s common to forget setting a new environment variable after deployment, so I’ll share a strategy that I’m using: 1- Use fetch method Let’s start with this example of missing NEW_KEY value:

So how this works? ENV is a hash-like accessor for environment variables so we can use hash methods but we need only fetch for the moment. Now…

Continue Reading
Tutorials

Set multiline ENV variable in production

Did you try to set up a private key as ENV variable in production but you get the following error:

So if you are setting your ENV variables through a terminal tool, use the following approach:

Next, we need to remove the escape character added when setting the key:

That’s it we have fixed this error and…

Continue Reading
Tutorials

work with Gems – how to update

In the first part, we saw some guidelines for choosing a Gem,  in the following post we will discuss updating gems which is an important use case for real world applications. When to update? Most of the available Gems in the ecosystem will receive some patches and updates. So we should check our installed versions for security fixes and performance improvement.…

Continue Reading
Tutorials

work with Gems – how to choose

Ruby provides a variety of Gems for every requirement and new developers get confused when choosing the between Gems. So I’ll share my personal guidelines for choosing a Gem: 1 – Check if the Gem is maintained:  You can verify if the author mentions in the documentation that this gem is not actively maintained for example Test Benchmark Gem, so in…

Continue Reading