Skip to content

Instantly share code, notes, and snippets.

@rafaellima
Created April 1, 2013 22:26
Show Gist options
  • Save rafaellima/5288299 to your computer and use it in GitHub Desktop.
Save rafaellima/5288299 to your computer and use it in GitHub Desktop.
Some thoughts about the law of Demeter
BAD SMELL
class Invoice < ActiveRecord::Base
belongs_to :user
end
<%= @invoice.user.name %>
<%= @invoice.user.email %>
Refactor
class Invoice < ActiveRecord::Base
belongs_to :user
delegate :name, :email, :to => :user, :prefix => true
end
<%= @invoice.user_name %>
<%= @invoice.user_email %>
@paulopatto
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment