There is a much easier solution to all of this.  Utilize the try()
method that comes with ActiveSupport:

<p>
<%= @post.try(:user).try(:name) %>
</p>

try() calls a method and catches exceptions, returning nil instead.
Try is added to the main Object class, so even nil objects have the
method, hence your ability to chain them together....


-casey

On Jun 29, 5:50 am, Michael Pavling <pavl...@gmail.com> wrote:
> On 29 June 2010 10:37, Carsten Gehling <li...@ruby-forum.com> wrote:
>
> > Yup it is a very neat solution, that I will adapt to. Thanks
>
> No worries. I got it from the "Refactoring: Ruby Edition" book - they
> call it the "introduce null object" pattern.
> Well worth being familiar with the book for loads of other very good
> little patterns.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to