5 secret RoR tips you didn’t know

5 secret RoR tips you didn’t know
Average rating: 0
(0 votes)

Thanks! You’ve rated this material!

Every week we share with you some interesting information about Ruby on Rails and its secrets. Because we are Ruby on Rails agency! Recently you could find the article about verifying the quality of Ruby on Rails development agencies. This time let us introduce you some useful tips for working with our favorite framework. Read 5 secret RoR tips you didn’t know.

If you worked with Ruby on Rails you might agree that it is the developer’s delight. It was created for smooth sailing work with no developer’s headache. What is interesting, this framework, just like Ruby itself, constantly receives some new improvements. And you discover something new almost every day.

We will not talk about “must-have” things but we will give you some useful tips to make the code cleaner. In this material, you will find a bunch of interesting recently discovered stuff.

1. Hash#dig

This race used method was introduced in Ruby version 2.3. How many times did you do something like this:

... if params && params && params

Think of dig as kind of safe navigation operator &. but for Hash objects. So now you could rewrite such things:

... if params.dig(:user, :address, :somewhere_deep)

2. Object#presence_in

This method allows you to replace conditionals with single method call when you don’t actually need a boolean result of inclusion check, but a checked object itself. For example:

sort_options = 
sort = sort_options.include?(params) 
  ? params 
  : :by_date
# Another option
sort = (sort_options.include?(params) && params) || :by_date

Doesn’t this look better?

params.presence_in(sort_options) || :by_date

3. Module#alias_attribute

It might be useful if you work on a project with a legacy database. It has a table with column names like SERNUM_0ITMDES1_0and other. By mapping this table to an ActiveRecord model and instead of writing queries and scopes on it like WeirdTable.where(SERNUM_0: ‘123’), you can use alias_attribute. It doesn’t just generate getter and setter but works in queries as well:

alias_attribute :name, :ITMDES1_0
scope :by_name, -> (name) { where(name: name) }

4. Object#presence

This one is more popular than others. You can find its explanation on ApiDock. So, object.presence is equivalent to:

object.present? ? object : nil

5. Module#delegate

Still rarely used by most of the developers for some reason. The main purpose of this method is loose coupling and following the Law of Demeter. The snippet below describes utilizing delegate in context of applying this law.

class Profile < ApplicationRecord
  belongs_to :user
  delegate :email, to: :user
profile.email # equivalent to profile.user.email


By the way, we are looking for senior Ruby on Rails developer.

Even if you are not RoR developer it’s always nice to know such things. You can share this with your development team and use for the future. But if you don’t have such team, you can hire Syndicode Ruby on Rails developers that know even more tips like this. Contact us about RoR development!
Subscribe to our weekly newsletter!

Rate this article, if you like it

Thanks! You’ve rated this material!

Got a project? Let's discuss it!

*By submitting this form you agree with our Privacy Policy.

Mailing & Legal Address

Syndicode Inc. 340 S Lemon Ave #3299, Walnut CA, 91789, USA

Visiting & Headquarters address
Kyiv Sofiivska 1/2a, 01001, Kyiv, Ukraine
Dnipro Hlinky 2, of. 1003, 49000, Dnipro, Ukraine
Email info@syndicode.com
Phone (+1) 9035021111