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. 🎯

83d09280c8809460222474ef8f7dbc94ee39abc9


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.