Rails. Source: www.usapics.net

Design Patterns: Law of Demeter with Rails

Developing better Rails applications

Being a good rails developer is not just to know some basic stuff like creating model, controller and migration. Almost every project tends to become more complicated and that’s why it’s necessary to know design patterns to avoid complication of code and time waste while refactoring.

Here we’ll talk about Law Of Demeter, very powerful and flexible pattern. Let’s see what is that Law and how it simplifies developer’s life.

I suppose you’re already familiar with ActiveRecord, brilliant tool. If you have Employee, which belongs to Company, and Company has Location, you will easily access methods of related objects from Employee instance:

Simple as that. Very popular approach used by many rails developers. But there are number of reasons why this code is not good:

  1. What if you will need to change name ‘location’ to ‘address’? In that case you must change it everywhere in code one by one.
  2. What if some of related objects are nil? NoMethodError guaranteed.
  3. And do you like that long line of code after all? I’m sure you don’t. It’s called ‘Multi dot syndrome’.

The alternative

And here the Law Of Demeter comes to play, which says:

Use only one dot.

and according to that advise, code above should look like this:

Diving deeper

We have 3 models and relationships between them: Company, Employee and Location

As we said, usage of several dots violates the Law. We need some short methods. Rails has very powerful thing for that called Delegation:

Simply say, delegation allows you to use methods of one object from another.

Something like this:

To achieve that, add delegate method to Employee model:

Here we’ve specified method name that we want to use (:name), name of the class where we want to use method from (:company), and a prefix to use :name method like company_name.

Also you can specify prefix: true. In that case delegate will look for a class that we’ve passed to :to hash and use that name as a prefix. But I prefer to specify something directly instead of just true and i’ll explain why little later.

With this approach you can use all methods from :company in Employee instance. If we will create some new method :phone in Company and pass it to delegate in Employee class:

we will be able to use it exactly the same way as the :name

Brilliant, isn’t it ?

Company’s location info

Besides company’s name, we also retrieving company location info. To achieve that we need to delegate Location’s methods in Company model:

Now Company can use this methods directly. But as you remember, we need to use these methods from Employee instance, and that’s why we should delegate :city, :street, :zip_code methods also in Employee model:

After that you can easily use all these methods from Employee instance. This is how code looks like after refactoring:

Why i not recommend prefix: true

We know prefix: true gets class name provided to :to hash and uses it as a prefix. But what if I want to change name from Company to Cmpn? In that case prefix: true will use ‘cpmn’ as a prefix and methods will look like this:

and that’s why NoMethodError will be raised. This is why I prefer to use something like prefix: ‘company’. Now if Company model will be renamed, we still will be able to use our methods without change.

What if some method name will be changed?

If you decided to change Location’s method street to str, NoMethodError will be raised. Solution in that case is to create new method street or an alias in Location model:

and delegate will look for :street, but will get data from :str method.

What if some of related objects is nil?

If Company or Location object is nil, we will have NoMethodError raised. We can avoid that by specifying allow_nil: true in delegate method:


As you can see, Law Of Demeter and Delegation are very easy, powerful and flexible. In my opinion they must be used in every application, doesn’t matter how small or big apps are.

Good luck and never violate Law of Demeter!

Web-developer, entrepreneur, researcher

Web-developer, entrepreneur, researcher