According to the law of demeter, a model should only talk to its immediate association, don’t talk to the association’s association and association’s property, it is a case of loose coupling.
1 2 3 4 5 6 7
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.
1 2 3 4 5 6 7 8
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.