Better understand Delegate in Rails

Tram Ho


In this article I will explain the delegate method in Ruby on Rails, the concept, the reason to use it and the proper usage through small examples.


What is delegate?

According to the official API description:

Provides a delegate class method to easily expose contained objects’ public methods as your own.

Provide the object with a delegate class method to easily call the public methods of other objects just like that object itself.

Why should you use Delegate?

Demeter’s law is a design principle for software development, especially object-oriented programs. This law is stated as follows:

Minimize the understanding of an object about the structure and properties of objects other than it (including children).

This makes the components in the system less dependent on each other, easier to pack and reuse.

The delegate method in Rails helps us to apply this law by only allowing access to the necessary methods of the parent object and thereby, making access to their properties easier.

How to use Delegate?

For example, we have 2 ActiveRecord models that have has_many relationship as follows:

Open up the rails console and see what these two properties have:

Now we will get the name and description of GiftBox through KitKat , usually we will do the following:

So we have obtained the name and description through the relationship with the GiftBox

But if we use delegate method, we can get name and description more easily and conveniently. Please revise the KitKat model a bit.

By using delegates, I have made the name and description of GiftBox as KitKat methods already. From now on we can get them without having to call :gift_boxs anymore by the following:


But what if the KitKat model itself has the name method? At that time we need to use the option :prefix :

We can then call the two method name as follows:

We can custom name the custom prefix, such as prefix: :show


If the object in :to has a value of nil , it will throw an Exception:

To avoid returning Module::DelegationError , we can add the allow_nil: true option to the delegate :


Chia sẻ bài viết ngay

Nguồn bài viết : Viblo