Is there a practical and not too-convoluted way to implement null objects that can be used in the
link_to helper on par with their “real” counterparts? I’m specifically thinking about associations, so that if there is
class Sponsor has_many :sponsorships def most_recent_active_sponsorship sponsorships.active.last || NullSponsorship.new end end
then in the view it would be possible to get rid of the
<p>Most recent active sponsorship:</p> <% sponsorship = @sponsor.most_recent_active_sponsorship %> <%# if sponsorship %> <%= link_to sponsorship.name, sponsorship %> <%# end %>
Using the null object in the
link_to should render the return value of its
#name method (e.g. “none”) as something clearly unclickable.