Rails 7.1 adds authenticate_by when using has_secure_password

Rails 7.1 introduces a method authenticate_by, used with has_secure_password to prevent timing-based enumeration attacks.

Rails 7 auto detects inverse_of for scoped associations

Rails framework is famous for developers' happiness and making things simpler due to its magic, provided developers follow proper conventions. To extend this magic and to make things simple further, Rails 7 has introduced a change with this PR after which, inverse_of would be inferred automatically for model associations having scopes. In this article, we'll dive into understanding it with examples. Let's say we have a Project model with many assigned tasks. # app/models/project.rb class Project < ActiveRecord::Base has_many :tasks, -> { assigned } end # app/models/task.rb class Task < ActiveRecord::Base belongs_to

Rails 7 - Associations across databases with disable_joins

Rails 7 allows to fetch data from multiple databases for a has_many/has_one:through association using the disabled_joins option.

Implement Passwordless Authentication via Magic Link in Rails API

Authentication is one of the key aspects of many web applications. It is the process of identifying a person before granting them access to the application. It is very important that the authentication approach is secure and easy to use for all users. What is passwordless authentication? Passwordless authentication is a verification process that determines whether an individual is who they claim to be, without coercion. You do not require credentials to log in. All you need is an email address or phone number associated with an account and you will get a magic link or one-time password each time

Alternative ways to include NULL values in the results while using SQL negation commands(!= or NOT IN) with Rails

In the previous article, we went through How NOT IN works with NULL values. We also learned how we can overcome this restriction. In this article, we will look at alternative ways to handle NULL values with SQL functions. So basically when we use != or NOT IN in query, it ignores the records with NULL values for fields. User.where("state != ?", 'active') SELECT * FROM users WHERE state != 'active' OR User.where("state NOT IN (?)", ['active']) SELECT * FROM users WHERE state NOT IN ('active') The above queries will consider records with state having a NOT NULL value,

How we improved the process of Code and Functional Reviews

Code reviews are important to deliver quality and well-crafted software. But what if the PR lingers in the review queue waiting for reviewers for an extended period? We faced a similar issue in one of our team where few stories were delayed due to pending reviews from the members. In this article, I would like to share about how we improved the Code and Functional Review process resulting in high quality and happiness. *This process was identified and implemented for one of the projects and every project could have minor customization based on the nature and requirement of the project.

How NOT IN works with NULL values.

Recently I faced one scenario with NULL value with NOT IN, I have added the details here about what I learnt. suppose we have two table Category and Product Category id | name -------- 1 | Category 1 2 | Category 2 3 | Category 3 4 | Category 4 Product id | name | category_id ----------------------------- 1 | Product 1 | 1 2 | Product 2 | 2 3 | Product 3 | 2 4 | Product 4 | 3 5 | Product 5 | 4 6 | Product 6 | NULL Scenario :- *I just want to retrieve all the Products excluding Category 1 and Category 3 so I wrote following query :- Product.joins(:category)

Export data to CSV file from Rails console

In one of my project I would like to dump and export some information into a csv file. I have produced an easy script to do that. require 'csv' file = "#{Rails.root}/public/user_data.csv" products = Product.order(:first_name) headers = ["Product ID", "Name", "Price", "Description"] CSV.open(file, 'w', write_headers: true, headers: headers) do |writer| products.each do |product| writer << [product.id, product.name, product.price, product.description] end end Here is what the above snippet does: Require the CSV library, since we'll be manipulating

Caching with Redis

What is Redis? Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. Why should we use Redis? By default, Rails uses the database to cache internal application data which can be expensive to generate (menu trees, views, filter results, etc), and to keep cached page contents. Since the database also handles many queries for normal page requests, it can create a bottleneck and increase load-times. Redis provides an alternative caching backend for Rails, taking that work off the database, which is vital for scaling to a larger number of logged-in

Attaching images using Attachinary and Cloudinary

A] Cloudinary: Cloudinary is a cloud service that offers a solution to a web application's entire image management pipeline. It is used to easily upload images to the cloud. It automatically performs smart image resizing, cropping and conversion without installing any complex software. Also, integration of Facebook or Twitter profile image extraction in a snap,that too in any dimension and style to match your website’s graphics requirements. Add following line to your Gemfile: gem 'cloudinary' Add following line to config/cloudinary.yml development: api_key: //enter api key api_secret: //enter api secret cloud_name: //enter cloud name