# Nil column results, possible to ignore?

**URL:** <https://www.ruby-forum.com/t/nil-column-results-possible-to-ignore/60405>\
**Category:** Rails\
**Created:** [January 9, 2006, 3:34am UTC](https://www.ruby-forum.com/t/nil-column-results-possible-to-ignore/60405 "2006-01-09T03:34:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmarkow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@dmarkow](https://www.ruby-forum.com/u/dmarkow)\
**Post date:** [January 9, 2006, 3:34am UTC](https://www.ruby-forum.com/t/nil-column-results-possible-to-ignore/60405/1 "2006-01-09T03:34:39Z")

</div>

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

---

<div class="post-metadata">

**Author:** ![dmarkow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@dmarkow](https://www.ruby-forum.com/u/dmarkow)\
**Post date:** [January 9, 2006, 7:54am UTC](https://www.ruby-forum.com/t/nil-column-results-possible-to-ignore/60405/2 "2006-01-09T07:54:21Z")

</div>

On 1/8/06, Dylan M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The following code throws a “You have a nil object” error when outputting my  
> client list. Is there a way around this, short of writing a specific case  
> for each possibility?
> 
> \<%= client.city + ', ’ + client.state + ’ ’ + client.zip + ‘  
> ’%\>

Try: \<%= “#{client.city}, #{client.state} #{client.zip}  
”%\>  
Or: \<%= client.city %\>, \<%= client.state %\> \<%= client.zip %\>

---

<div class="post-metadata">

**Author:** ![dmarkow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@dmarkow](https://www.ruby-forum.com/u/dmarkow)\
**Post date:** [January 9, 2006, 11:10am UTC](https://www.ruby-forum.com/t/nil-column-results-possible-to-ignore/60405/3 "2006-01-09T11:10:49Z")

</div>

> For a table “clients” there are city, state, and zip fields.  
> However, for some clients we only have a state with no actual  
> address. The following code throws a “You have a nil object” error  
> when outputting my client list. Is there a way around this, short  
> of writing a specific case for each possibility? \<%= client.city +  
> ', ’ + client.state + ’ ’ + client.zip + ‘  
> ’%\>

A neat thing to do in views is the following (which not quite fits  
your case, but would help if you have the possibilty of the client  
object being nil)

\<%= client.city rescue “no city” -%\>

I ususally use this for objects that are compounded via has\_…  
relationships:

\<%= client.city.name rescue “no name defined” -%\>

this is the short form of a

begin  
do some stuff  
rescue  
there was an exception, let’s deal with it  
end

clause

hth jc

---

<div class="post-metadata">

**Author:** ![dmarkow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@dmarkow](https://www.ruby-forum.com/u/dmarkow)\
**Post date:** [January 9, 2006, 11:31am UTC](https://www.ruby-forum.com/t/nil-column-results-possible-to-ignore/60405/4 "2006-01-09T11:31:58Z")

</div>

> A neat thing to do in views is the following (which not quite fits  
> your case, but would help if you have the possibilty of the client  
> object being nil)
> 
> \<%= client.city rescue “no city” -%\>
> 
> I ususally use this for objects that are compounded via has\_…  
> relationships:  
> It could be more MVC if that rescue thing was placed in the model,  
> wasn’t it? Do you think is it possible?  
> Just food for though… 🙂

–  
“The only thing necessary for the triumph of evil  
is for good men to do nothing”  
Edmund Burke

---

<div class="post-metadata">

**Author:** ![dmarkow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@dmarkow](https://www.ruby-forum.com/u/dmarkow)\
**Post date:** [January 9, 2006, 8:27am UTC](https://www.ruby-forum.com/t/nil-column-results-possible-to-ignore/60405/5 "2006-01-09T08:27:31Z")

</div>

On 1/8/06, Jeremy E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

I’m going to briefly explain why you need to do it like this, and  
hopefully you can avoid problems in the future.

When one of the fields is nil, you end up with … nil + … and of  
course there is no + operator defined for a nil. If you include them  
directly in the string using #{client.state} format as shown above,  
you never end up using an operator - which is simply invoking a method  
on an object - on a nil.

Pat

---

<div class="post-metadata">

**Author:** ![dmarkow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@dmarkow](https://www.ruby-forum.com/u/dmarkow)\
**Post date:** [January 9, 2006, 1:56pm UTC](https://www.ruby-forum.com/t/nil-column-results-possible-to-ignore/60405/6 "2006-01-09T13:56:46Z")

</div>

> Just food for though… 🙂  
> I’m not sure that’s the case (but willing to be convinced otherwise).  
> I usually see this code when I don’t have complete control over the  
> data in the database but still need to be sure, that something  
> sensible is displayed. Usually, the controller gives me a bunch of  
> objects and some of them (or some of their relations) can be NIL. I’m  
> not sure how that could be handled by the model…

---

<div class="post-metadata">

**Author:** ![dmarkow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@dmarkow](https://www.ruby-forum.com/u/dmarkow)\
**Post date:** [January 10, 2006, 5:54am UTC](https://www.ruby-forum.com/t/nil-column-results-possible-to-ignore/60405/7 "2006-01-10T05:54:48Z")

</div>

Enrico T. wrote:

> wasn’t it? Do you think is it possible?  
> Just food for though… 🙂

I disagree… what to print if an object has no value is a display  
issue… in fact, I  
think it’s a common use case to want to display different things on  
different pages for  
the nil case… or nothing as previous posters have pointed out (by  
using #{}).

b
