Ruby on Rails Unknown Inbuilt Features
π Ruby on Rails: Unknown Inbuilt Features You Wish You Knew Earlier! ππ»
Ruby on Rails (RoR) is famous for its developer happiness, but behind the scenes, it hides a treasure chest of lesser-known inbuilt features that can make your code cleaner, faster, and smarter. π‘
In this blog, weβll explore some hidden gems of Rails β from magical ActiveRecord tricks to time-saving helpers β complete with examples and bonus hacks to level up your coding game. π―
1οΈβ£ .pluck
β Fetch Data Without the Baggage πͺ
Need only certain column values? Instead of fetching entire ActiveRecord objects (which is slower), use .pluck
!
# Without pluck
User.all.map(&:email)
# Fetches all data into memory β wasteful π©
# With pluck
User.pluck(:email)
# Returns array of emails directly from DB π
Why itβs great: Speeds up queries and reduces memory usage. Perfect for large datasets!
2οΈβ£ try
β Safe Method Calls Without Conditionals π‘οΈ
Avoid long nil
checks and make your code elegant.
# Without try
username = user && user.profile && user.profile.username
# With try
username = user.try(:profile).try(:username)
Hack: In Rails 5+, you can also use the safe navigation operator:
username = user&.profile&.username
3οΈβ£ find_each
β Batch Processing Without Memory Explosions π¦
When dealing with thousands (or millions) of records, .each
will load all of them into memory. Bad idea.
Use .find_each
to load them in batches.
User.find_each(batch_size: 1000) do |user|
UserMailer.newsletter(user).deliver_later
end
Why itβs great: Prevents your app from crashing with Out of Memory errors.
4οΈβ£ .in_batches
β Even More Batch Power ποΈββοΈ
Want to update or delete in chunks directly in DB?
User.in_batches(of: 500).update_all(active: true)
Hack: Unlike find_each
, this doesnβt instantiate objects, so itβs super fast! β‘
5οΈβ£ .where.not
β The Anti-Query π«
Rails has a .not
query builder that makes exclusions simple.
User.where.not(status: 'inactive')
Why itβs great: More readable and less error-prone than writing raw SQL !=
.
6οΈβ£ touch
β Auto-Update Timestamps Without Changing Data β°
Need to refresh updated_at
without changing actual content?
post.touch
# updates post.updated_at to current time
Pro Tip: Great for cache invalidation or triggering ActiveRecord callbacks.
7οΈβ£ reload
β Refresh From DB Instantly π
If your object might be stale, reload it directly from DB:
user.reload
Why itβs great: Avoids stale data when another process updates the record.
8οΈβ£ find_or_create_by
β One-Liner Record Lookup π―
Stop writing boilerplate to check existence before creation.
User.find_or_create_by(email: "test@example.com") do |user|
user.name = "Test User"
end
9οΈβ£ Rails Time Helpers β Natural Language Dates ποΈ
Rails understands plain English for time calculations:
3.days.ago # => 2025-08-07
2.weeks.from_now # => 2025-08-24
Bonus: Works with hours, months, years, etc.
π .except
& .slice
β Smart Hash Filtering π§Ή
Perfect for params cleanup.
params.except(:password, :token) # removes keys
params.slice(:name, :email) # keeps only keys
π Bonus Hacks & Tricks
π‘ 1. Use rails db:seed_fu
(via seed-fu gem) β For idempotent seed data.
π‘ 2. Use Rails.logger.debug
instead of puts
for production-friendly logging.
π‘ 3. Chain .select
and .map
carefully β Use .pluck
instead for speed.
π‘ 4. Use delegate
in models to clean up method forwarding.
class Order < ApplicationRecord
belongs_to :customer
delegate :name, :email, to: :customer, prefix: true
end
order.customer_name # direct access!
π‘ 5. Use .presence
to shorten nil/blank checks:
name = params[:name].presence || "Guest"
β¨ Final Thoughts
Rails isnβt just about conventions over configuration β itβs about hidden superpowers waiting to be unlocked. By mastering these inbuilt features, youβll write cleaner, faster, and smarter code. π
Next time youβre coding, try using one of these tricks β you might just wonder how you ever lived without it. π
© Lakhveer Singh Rajput - Blogs. All Rights Reserved.