present? vs exists? in Rails
present? and exists? serve the same purpose, i.e. to check if a record exists in the database or not. They do, however, differ in the performance.
present? and exists? serve the same purpose, i.e. to check if a record exists in the database or not. They do, however, differ in the performance.
This article will discuss key differences between update_all and update in Rails.
This article will discuss two important concepts in Ruby on Rails - Named Scopes and Default Scopes. The main idea is to get results based on some condition.
This article will discuss the differences between select and pluck methods for ActiveRecord in Rails.
This article will discuss how to create dynamic nested forms in Ruby on Rails using Cocoon gem. We will use two models, product and FAQ. The product can have multiple FAQs, which we will add manually while creating it.
Sometimes you need to create password-protected zip files in Ruby on Rails. The idea is to create a CSV file first and then add it to a zip file that will be encrypted. For instruction on generating a CSV file, please read this post.
In this article, we will discuss an easier way to search and sort in Rails, using a gem called ransack.
This article will discuss how to export data as a CSV from the Rails controller by using formats. First, set up the application and seed in some data.
This article will discuss how to implement several helpful string methods in Ruby on Rails: camelize
, classify
, humanize
and inquiry
.
When upgrading a legacy Rails app that has buttons/links that trigger raw ajax instead of remote: true
for making javascript format requests, adding remote: true
can make things simpler.
Before Rails 5, all constants were loaded via ‘autoloading’ when the application booted up. Rails 5 replaced this with ‘eager loading,’ i.e loading all the constants even before they are actually needed.
With ‘autoloading,’ the application does not load the constant until it is needed. Once a class is needed, if it is missing then the application starts looking in ‘autoload paths’ to load it. Eager_load_paths
contains a list of directories. When the application boots then it loads all constants found in all directories listed in eager_load_paths.
There are many use cases that require an href link to be applied to an entire div. One such situation required an href to be applied to this entire icon in order to make it clickable:
Sometimes in Rails applications we need to perform an action based on a change in a particular attribute of a model.
When an exception occurs, the goal is always to handle it gracefully. A red page full of errors appears extremely unprofessional.
While updating ActiveStorage attachments, particularly those with the has_many_attached association in a Rails application, some common issues can arise. This article will discuss some such issues and their possible solutions.
In this article we will be explaining the difference between includes and joins in Rails ActiveRecord, a topic which often confuses newbies to Rails.
If you need to update an active record attachment that has been attached using has_one_attached relation with the parent model, you may encounter errors. Here’s an example:
This article will discuss HTTP Basic Authentication in Ruby on Rails. This can be useful when a page needs to be accessible only to users with a password.
This article will discuss how to bypass password and password confirmation in the registration process and send a password through email when a user registers themselves.
This article will discuss implementing STI(Single Table Inheritance) using Devise. First, create a project.
This article will discuss how to implement an admin panel with activeadmin in Ruby on Rails. First, create a project and a few models.
This article will discuss bullet
gem which helps you watch n + 1 queries when they are being used unnecessarily, as well as helping you to determine when they should be used.
Keeping technical documentation handy is extremely useful, especially when a project gets huge in terms of the code and database. One of the documents that should be included in the documentation is an Entity Relationship Diagram, or ERD. This article will discuss how to generate ERD at any stage of a project.
This article will discuss how to implement JBuilder for creating JSON response in Ruby on Rails.
This article will discuss how to implement charts and graphs in Ruby on Rails 6 applications.
This article will discuss how to implement authorization in a Ruby on Rails application using CanCanCan. Here’s how to start:
This article will discuss how to integrate AWS S3 in a Ruby on Rails application using ActiveStorage.
This article will discuss how to scrape websites in Ruby and Selenium. CSS class selectors will be used to scrape the data.
This article will discuss how to implement multiple bootstrap themes in Ruby on Rails 6 applications. This article is a continuation of this article, which explains how to implement a single theme.
This article will discuss how to implement token-based authentication using a gem called devise_token_auth
. Here’s how to get started:
This article will discuss how to integrate VUE JS in a Ruby on Rails 6 application. Here’s how to start:
This article will explain how to use a bootstrap theme in a Ruby on Rails 6 application using AdmineLTE3. Start like this:
Frequently developers need to write data to files like .csv and .xlsx. This article will discuss this common use case in Ruby on Rails applications.
Imagine you have a fruit shop application built in Ruby on Rails. As a Rails developer, you know that to get to a specific fruit page, a URL like www.myfruitshop/1
will work - but it’s more user friendly to use something like this:
This article will discuss how to implement Ajax in a Rails 6 application. Here’s how to start:
As any Ruby on Rails developer knows, webpacker was a major addition in Rails 6. With the introduction of webpacker, Rails applications no longer need asset pipeline. The app/assets/javascript
is moved to app/javascript
.
This article will discuss how to attach images to a model from URLs with ActiveStorage.
Conditional validations can be used when a validation needs to be run only if a condition is satisfied. This can be implemented in many ways, a few of which we will discuss in this article.
It’s common to need to read data from .csv or .xlsx files and insert that data into the database. This is a frequent use case in web development. In this article, we will discuss this use case in Ruby on Rails applications.
For Rails developers, using enums with ActiveRecord is always a good choice when you have to manage the state of an object. For example, for an online shop, you may have three states of an order - i.e. received
, dispatched
and delivered
. One option is to add three boolean fields in the database for received
, dispatched
and delivered
, but this approach looks ugly. A cleaner approach is to add a single string column status that includes values of received
, dispatched
and delivered
, then add three methods in the model file.
Many applications use mobile number authentication instead of email-based authentication. This article will discuss the technique to override default authentication behavior. Here are the steps to authenticate using a mobile number:
This article will discuss how to send an sms in a Ruby on Rails application using Twilio.
Locking in a database means that no two or more sessions can update the same record at the same time until the lock is released. In this article we will discuss two main types of locking in Ruby on Rails; Optimistic Locking and Pessimistic Locking.
This article will discuss a concept known as Custom Validations
in Rails Active Record. There are two types of custom validations; Custom Validators and Custom Methods.
This article will discuss database transactions and how to use them in Ruby on Rails. Transaction in a database means the state of the database will only change if all the statements in a transaction block succeed, otherwise all the statements will roll back and no state change will occur.
This article will discuss the most basic technique to speed up a Rails application by optimizing database queries.
This article will discuss how to make search functionality easier and faster in Ruby on Rails through a gem called pg_search
. According to the official documentation:
This article will discuss Polymorphic Association, an important concept in Ruby on Rails.
This article will discuss Single Table Inheritance in Ruby on Rails.
ActiveRecord AutosaveAssociation
is a module that is used for saving the child objects if the parent is saved and deleting the child objects if they are marked for destruction. This article will demonstrate how this works.
Often there are situations where there are separate menus/layouts for admin and other pages. This article will discuss layouts in Ruby on Rails. To start:
This article will discuss a very useful method in Ruby on Rails - ActiveRecord, i.e. delegate. According to the documentation, delegate:
Hash is a very common data structure used by Ruby developers. This article will discuss some important hash methods every Ruby developer should know.
This article will discuss Ruby array methods every Ruby developer should know. These methods are each
, map
, select
, reject
, select!
, reject!
, compact
and include?
. This article will also discuss the comparison of these methods where applicable.
With the increasing number of daily routine mobile/web applications people use, password management becomes an issue. Almost all applications now provide social login, but some people would prefer not to use that method.
When developing a Ruby on Rails application that involves a user having one address, and the address table having all the address values such address_line_1, address_line_2, etc., the details may need to be shown similar to this:
This article demonstrates adding ActionText to Rails applications. According to the official ActionText documentation:
Tailwind is a CSS framework that is quickly gaining popularity over other CSS frameworks. The reason for that, according to the creators of Tailwind:
Rails 6 introduced webpacker
, which they encourage use of now over the legacy asset pipeline
. This article explains how to get bootstrap, which is used by almost all applications, working with webpacker
in Rails 6.
The will_paginate
gem is useful in situations where you need to add paging support to a long list of results, such as an Orders list. By default, will_paginate
looks a little rough without any styling love. Here are the most useful styling tricks to save you some time.
Social login/registration options can make for a smoother user experience, so they are almost always integrated into Mintbit applications. This article explains how to add a login option through Google and the security measures required to keep your private keys secure in the environment
.
There are many use cases for achieving multitenancy in a Rails application. Two of the best gems to use for this purpose are apartment and acts_as_tenant. Both work well and are commonly used. Here are some tips for using acts_as_tenant
along with Devise, the most common user authentication gem, to create a multitenant Rails application
Mintbit always keeps an eye on changelogs from Ruby, Rails and many commonly used gems, and tries to follow the recommendations and avoid deprecations.
Rails introduced ActiveSupport::CurrentAttributes in version 5.2. Since its addition there has been a lot of discussion about it, with advocates both for and against it making valid points. The merits of ActiveSupport::CurrentAttributes actually depend heavily on how it is used.
Assume you have a large or mid-level organization where a repository is shared by multiple developers and they often push the code during a day. Let’s say, a developer was building a feature and was coding in a class/module that was being called by ten other places, coded by other developer(s). Let’s also assume that he has completed his feature, tested it and it’s ready for production and it is pushed to the repository and deployed. But he might not be aware of the fact that he has changed the code that was being called by several other modules, and now, let’s say if not all, 2 to 3 of the places are broken and no one knows about it until end-user hits the problem, report it, then the developers fix the code for those 2 to 3 places, this might end up causing problems in other places where it was being called, and you end up in total chaos and it also multiplies the cost of development.
Software development practices change with time and the practices that were used before are being continuously replaced by some new practices that we call “Best Practices”. In this article, we will discuss something similar that is related to refactoring the code in a Rails application following SRP (Single Responsibility Principle).
Just about every application inevitably needs to support file uploads of some kind. For example, a user might need to associate a profile picture with their bio, or notes may be more helpful if attachments could be attached, etc. Cloudinary can be a great choice, as it’s quick and easy to implement, and handles a lot of the heavy lifting for us, including integration with various cloud storage providers.
A common approach to modern Ruby on Rails development includes continuous integration checks, such as: