# Prettier code in views for chained values?

**URL:** <https://www.ruby-forum.com/t/prettier-code-in-views-for-chained-values/112830>\
**Category:** Rails\
**Created:** [August 23, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/prettier-code-in-views-for-chained-values/112830 "2007-08-23T17:16:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mario\_TSL](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@Mario\_TSL](https://www.ruby-forum.com/u/Mario_TSL)\
**Post date:** [August 23, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/prettier-code-in-views-for-chained-values/112830/1 "2007-08-23T17:16:32Z")

</div>

Sometimes in a view I have written wretchedly, ugly code like this:

\<%= @person.company.address.city if @person and @person.company and  
@person.company.address and @person.company.address.city %\>

I’m calling this a chained value. These values can run deep whether  
they are the part of an ActiveRecord association (e.g. a child record)  
or a simple composite field (e.g. address\_city and address\_state become  
address.city and address.state).

In any case, we have to make sure everything exists before we try to  
display a deeply chained value. Otherwise, our app barfs.

Before I go reinventing the wheel, I am wondering how others may have  
handled this, perhaps with a generic helper method. I am considering  
writing one. I would want it to be pretty to look at, maybe something  
along these lines:

\<%= chained\_value @person.company.address.city %\>

Trouble is, I don’t think this will work because I am guessing it will  
attempt to resolve the value it intends on passing. I could describe  
the chain and handle it with an eval, I guess, but it looks less pretty:

\<%= chained\_value “@person.company.address.city” %\>

The method definition might look something like this:

def chained\_value(value\_chain, no\_value = “”, error\_value = “N/A”)

It has a value it displays when the last item in the chain is blank, and  
one it displays if it chokes while trying to work it’s way down the  
chain.

Your thoughts?  
Mario T. Lanza

---

<div class="post-metadata">

**Author:** ![Mario\_TSL](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@Mario\_TSL](https://www.ruby-forum.com/u/Mario_TSL)\
**Post date:** [August 23, 2007, 5:33pm UTC](https://www.ruby-forum.com/t/prettier-code-in-views-for-chained-values/112830/2 "2007-08-23T17:33:59Z")

</div>

Hi,

Mario T. Lanza wrote:

> Sometimes in a view I have written wretchedly, ugly code like this:
> 
> \<%= @person.company.address.city if @person and @person.company and  
> @person.company.address and @person.company.address.city %\>

you can try

\<%= @person.company.address.city rescue nil %\>

That way, if everything’s fine you get the value or else you get  
nothing.

regards,

javier ramírez

---

<div class="post-metadata">

**Author:** ![Mario\_TSL](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@Mario\_TSL](https://www.ruby-forum.com/u/Mario_TSL)\
**Post date:** [August 23, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/prettier-code-in-views-for-chained-values/112830/3 "2007-08-23T17:51:48Z")

</div>

Thanks, Javier! I knew there had to be a better way.

I did, however, go ahead and solve the problem with this:

def chained\_value(no\_value = “”, error\_value = “N/A”)  
begin  
value = yield  
value.blank? ? no\_value : value  
rescue  
error\_value  
end  
end

alias :cv :chained\_value

This allows for one extra thing: a substitute value to use when blank.

\<%= cv(“No City”, “No Company Address”){@person.company.address.city} %\>

Plus it’s just as concise:

\<%= @person.company.address.city rescue nil %\>  
\<%= cv{@person.company.address.city} %\>

---

<div class="post-metadata">

**Author:** ![Mario\_TSL](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@Mario\_TSL](https://www.ruby-forum.com/u/Mario_TSL)\
**Post date:** [August 23, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/prettier-code-in-views-for-chained-values/112830/4 "2007-08-23T18:00:43Z")

</div>

Alternately:

def chained\_value(no\_value = “”, error\_value = “N/A”)  
yield || no\_value rescue error\_value  
end

Contrast

\<%= cv(“No City”, “No Company Address”){@person.company.address.city} %\>

with

\<% @person.company.address.city || “No City” rescue “No Company Address”  
%\>

Just experimenting with the syntax.
