14 Hidden Ruby on Rails Methods You Should Use for Cleaner Smarter Code
π 14 Hidden Ruby on Rails Methods You Should Use for Cleaner, Smarter Code!
Writing clean and elegant code is not just a skillβitβs a signature of a great Rails developer. Ruby on Rails hides some real gems πͺ behind its expressive syntax. Many developers miss these powerful methods that make your code shorter, more readable, and highly maintainable.
In this blog, youβll discover 14 hidden Rails & Ruby methods that can instantly level up your code qualityβwith clear examples. Plus, youβll get actionable clean code tips at the end.
Letβs unlock them ππ
πΉ 1. presence β Smart Nil or Blank Handling
Instead of writing long conditional checks, use presence to return value only if itβs not blank.
β Old Way
name = params[:name]
name = nil if name.blank?
β Clean Way
name = params[:name].presence
β Why?
Saves time, removes boilerplate, improves readability.
πΉ 2. try β Safe Method Calling
Avoid nil errors when calling methods on possibly nil objects.
β Old Way
user && user.profile && user.profile.city
β Clean Way
user.try(:profile).try(:city)
β Tip
Use &. (Safe Navigation Operator) when possible:
user&.profile&.city
πΉ 3. compact_blank β Remove Empty Values Like a Pro
Works on arrays and hashes to remove nil, "", and blank values.
Example
["Ruby", "", nil, "Rails"].compact_blank
# => ["Ruby", "Rails"]
Hash Example
{ name: "Raj", age: nil, country: "" }.compact_blank
# => { name: "Raj" }
πΉ 4. delegate β Cleaner Model Methods
Avoid writing forwarding methods manually.
β Without delegate
def city
profile.city
end
β
With delegate
delegate :city, to: :profile
Bonus
delegate :city, :state, to: :profile, prefix: true
# => user.profile_city
πΉ 5. pluck β Faster DB Queries
Fetch only required columns.
Example
User.pluck(:email)
# => ["a@mail.com", "b@mail.com"]
Why?
Avoids loading full AR objects β faster, lighter memory usage β‘
πΉ 6. in_batches β Process Big Data Without Killing RAM
Perfect for large dataset operations.
User.in_batches(of: 1000) do |batch|
batch.update_all(active: true)
end
πΉ 7. transform_values β Clean Hash Transformations
{ a: 1, b: 2 }.transform_values { |v| v * 10 }
# => { a: 10, b: 20 }
πΉ 8. extract_options! β Cleaner Arguments in Methods
Rails automatically pulls hash options from array parameters.
def signup(*args)
options = args.extract_options!
puts options[:name]
end
signup(:admin, name: "Lakhveer")
πΉ 9. tap β Debug-Friendly, Elegant Chaining
user = User.new.tap do |u|
u.name = "Raj"
u.age = 25
end
Why?
Super useful for object setup + debugging pipelines.
πΉ 10. with_indifferent_access β Access Hash Keys as String or Symbol
params = { "name" => "Raj" }.with_indifferent_access
params[:name] # => "Raj"
params["name"] # => "Raj"
πΉ 11. slice β Extract Only Needed Keys
params.slice(:email, :password)
# Perfect for whitelisting
πΉ 12. except β Remove Unwanted Keys
params.except(:token, :signature)
πΉ 13. blank? vs present? β Cleaner Boolean Logic
if user.present?
# clean & expressive
end
πΉ 14. find_each β Load Records in Batches Automatically
Instead of:
User.all.each { ... }
Use:
User.find_each(batch_size: 1000) do |user|
puts user.email
end
Efficient, scalable, production-safe.
π‘ Clean Code Tips Every Rails Developer Should Follow
β¨ 1. Follow SLAP (Single Level of Abstraction Principle)
Each method should do only one thing.
β¨ 2. Use Service Objects, Decorators, Presenters
Keep controllers skinny and models clean.
β¨ 3. Use Meaningful Names
Variables like x, data, temp kill readability.
β¨ 4. Remove Dead Code
Unused methods, comments, files β clean them regularly.
β¨ 5. Follow Consistent Style
Use RuboCop + standardrb for consistency.
β¨ 6. Avoid Fat Models
Push logic into modules or service classes.
β¨ 7. Prefer early return over nested conditions
return unless user.present?
β¨ 8. Write Smaller, Focused Methods
Each method β€ 10β15 lines for readability.
π Conclusion
Rails is full of hidden gems π that make your code elegant, readable, and scalable. By using methods like presence, delegate, pluck, try, compact_blank, and tap, your code becomes more Ruby-ish and clean.
Write less, express moreβthatβs the Rails magic! ππ₯
© Lakhveer Singh Rajput - Blogs. All Rights Reserved.