title: Rails delegate method with more than one association
body: <p>The <code>delegate</code> method seems like a great alternative to doing nil checks all over the place in rails apps, but I am having trouble applying it to situations where I have more than one association.</p>
  
  <p>Consider these associations:</p>
  
  <pre><code>#app/models/user.rb
  class User &lt; ActiveRecord::Base
    belongs_to :blog
  end
  
  #app/models/blog.rb
  class Blog &lt; ActiveRecord::Base
    belongs_to :hash_tag
    has_one :user
  end
  
  #app/models/hash_tag.rb
  class HashTag &lt; ActiveRecord::Base
    has_one :blog
  end
  </code></pre>
  
  <p>I grab a user:</p>
  
  <pre><code>@user = User.find(1)
  </code></pre>
  
  <p>And I want to find his blog:</p>
  
  <pre><code>@user.blog
    =&gt; nil
  </code></pre>
  
  <p>It returns nil here because this <code>user</code> happens to have no associated <code>blog</code>, so the following code would break the application if I did something like this for this <code>user</code>:</p>
  
  <pre><code>@user.blog.title
    =&gt; undefined method `title' for nil:NilClass
  </code></pre>
  
  <p>So I could do this:</p>
  
  <pre><code>@user.blog.title if @user.blog.present?
  </code></pre>
  
  <p>But this is a nil check, and we want to avoid nil checks because otherwise they will be absolutely everywhere in the app.</p>
  
  <p>So you can do this which applies the law of demeter and works great:</p>
  
  <pre><code># open up app/models/user.rb
  class User &lt; ActiveRecord::Base
    belongs_to :blog
    delegate :title, to: :blog, prefix: true, allow_nil: true #add this
  end
  </code></pre>
  
  <p>And now we can do this, which is great because if the <code>user</code> does not have a <code>blog</code> then <code>nil</code> is just returned as opposed to that error: <code>undefined method 'title' for nil:NilClass</code>:</p>
  
  <pre><code>@user = User.find(1)
  @user.blog_title
  </code></pre>
  
  <p>Great, that works and we avoided the nil check.  But what if we want to grab the associated <code>hash_tag.tag_name</code>?  If nil was not an issue we could do this:</p>
  
  <pre><code>@user = User.find(1)
  @user.blog_title.hash_tag.tag_name
  </code></pre>
  
  <p>Not only would this break the law of demeter, but because nil <strong><em>is</em></strong> an issue and the associated hash_tag object may not exist, we again will one day run into the error: <code>undefined method 'title' for nil:NilClass</code>.</p>
  
  <p>I attempted to again open up the <code>User</code> class and add in a nested target for a delegate, but it was not working for me:</p>
  
  <pre><code># again: open up app/models/user.rb
  class User &lt; ActiveRecord::Base
    belongs_to :blog
    delegate :title, to: :blog, prefix: true, allow_nil: true
    delegate :tag_name, to: :'blog.hash_tag', prefix: true, allow_nil: true #doesn't work
  end
  </code></pre>
  
  <p>Apparently my syntax is off because it does not like my specifying this nested target as: <code>:'blog.hash_tag'</code>.</p>
  
  <p>What I want to do is to be able to say: <code>@user.blog_hash_tag_tag_name</code>.  Is this possible with delegate?</p>
  
  <p>I did review the <a href="http://apidock.com/rails/Module/delegate" rel="nofollow">Delegate documentation</a>.  I did not see it mentioning more than one association as is my current situation.</p>