Fork me on GitHub

the Law of Demeter

24 Jul 2010

Wen-Tien Chang

Bad Smell

class Invoice < ActiveRecord::Base
  belongs_to :user

<%= %>
<%= @invoice.user.address %>
<%= @invoice.user.cellphone %>

In this example, invoice model calls the association(user)'s property(name, address and cellphone), which violates the law of demeter. We should add some wrapper methods.


class Invoice < ActiveRecord::Base
  belongs_to :user
  delegate :name, :address, :cellphone, :to => :user, :prefix => true

<%= @invoice.user_name %>
<%= @invoice.user_address %>
<%= @invoice.user_cellphone %>

Luckily, rails provides a helper method delegate which utilizes the DSL way to generates the wrapper methods. Besides the loose coupling, delegate also prevents the error call method on nil object if you add option :allow_nil => true.