# How to deal with NULL columns when displaying db tables?

**URL:** <https://www.ruby-forum.com/t/how-to-deal-with-null-columns-when-displaying-db-tables/71140>\
**Category:** Rails\
**Created:** [October 5, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/how-to-deal-with-null-columns-when-displaying-db-tables/71140 "2006-10-05T15:59:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [October 5, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/how-to-deal-with-null-columns-when-displaying-db-tables/71140/1 "2006-10-05T15:59:34Z")

</div>

Hi,

I’m displaying a simple table from my model like :

\<% @people.each do |person| %\>

```
<tr>
    <td><%= h(person.email) %></td>
    <td><%= h(person.name) %>
    <td><%= h(person.adress) %>
    (...)
</tr>

```

\<% end %\>

The problem is that some columns are sometimes NULL and I get an  
exception.  
I’ve put “if person…” in each line but i don’t like that way (it does  
not seem to be a “DRY” way). What could I do ?

Thanks.  
Nicolas.

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [October 5, 2006, 4:14pm UTC](https://www.ruby-forum.com/t/how-to-deal-with-null-columns-when-displaying-db-tables/71140/2 "2006-10-05T16:14:55Z")

</div>

On 5 October 2006 17:59, Nicolas B. wrote:

> The problem is that some columns are sometimes NULL and I get an  
> exception.  
> I’ve put “if person…” in each line but i don’t like that way (it does  
> not seem to be a “DRY” way). What could I do ?  
> So, are you saying that you get an exception when you trying to get  
> attribute  
> value which is NULL ? That shouldn’t happen.

Also, I’ve checked h() method and it should handle NULLs fine.

Maybe you could provide exception error messages to examine ?

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [October 5, 2006, 5:03pm UTC](https://www.ruby-forum.com/t/how-to-deal-with-null-columns-when-displaying-db-tables/71140/3 "2006-10-05T17:03:01Z")

</div>

On 5 October 2006 18:38, Nicolas B. wrote:

> For example I’ve got one line like :
> 
> \<%= h(person.department.name) %\>
> 
> I get : “You have a nil object when you didn’t expect it!  
> The error occured while evaluating nil.name” if i don’t test  
> “person.department”.  
> In this situation there is no other way to avoid error unless you are  
> using  
> Edge Rails where there is a patch that allow Nils to execute any unknown  
> method with result of nil.

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [October 5, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/how-to-deal-with-null-columns-when-displaying-db-tables/71140/4 "2006-10-05T17:45:05Z")

</div>

Nicolas B. wrote:

> For example I’ve got one line like :
> 
> \<%= h(person.department.name) %\>
> 
> I get : “You have a nil object when you didn’t expect it!  
> The error occured while evaluating nil.name” if i don’t test  
> “person.department”.

Yes, for this example, you just have to write:

h(person.department.name) if person.department

> I don’t understand. If I want to write \<%= object.a.b.c.d %\> and c/d =  
> nil, writing \<%= object.a.b.c.d if object.a.b.c.d %\> won’t do it because  
> nil.nil generates an exception…

This is exactly why it’s generally bad style to chain these kinds of  
calls too deeply, particularly in views. You end up having to do lots  
of this kind of checking, and it gets messy. That sort of knowledge is  
better kept in the model itself, rather than ‘reaching through’ two or  
three objects, and having to deal with all the potential nils every  
time you do it.

For instance, you could have this method in Person:

def department\_name  
self.department.name if self.department  
end

And then it’s always safe to put this in your view:

h(person.department\_name)

This principle is known as the Law of Demeter:

> **[Law of Demeter](https://en.wikipedia.org/wiki/Law_of_Demeter)**
>
> The Law of Demeter (LoD) or principle of least knowledge is a design guideline for developing software, particularly object-oriented programs. In its general form, the LoD is a specific case of loose coupling. The guideline was proposed by Ian Holland at Northeastern University towards the end of 1987, and the following three recommendations serve as a succinct summary:
> The fundamental notion is that a given object should assume as little as possible about the structure or properties of anything...

Chris

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [October 5, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/how-to-deal-with-null-columns-when-displaying-db-tables/71140/5 "2006-10-05T16:38:03Z")

</div>

Maxim K. wrote:

> On 5 October 2006 17:59, Nicolas B. wrote:
> 
> > The problem is that some columns are sometimes NULL and I get an  
> > exception.  
> > I’ve put “if person…” in each line but i don’t like that way (it does  
> > not seem to be a “DRY” way). What could I do ?  
> > So, are you saying that you get an exception when you trying to get  
> > attribute  
> > value which is NULL ? That shouldn’t happen.
> 
> Also, I’ve checked h() method and it should handle NULLs fine.
> 
> Maybe you could provide exception error messages to examine ?

OK.

For example I’ve got one line like :

\<%= h(person.department.name) %\>

I get : “You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.name” if i don’t test  
“person.department”.

I don’t understand. If I want to write \<%= object.a.b.c.d %\> and c/d =  
nil, writing \<%= object.a.b.c.d if object.a.b.c.d %\> won’t do it because  
nil.nil generates an exception…

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [October 5, 2006, 5:51pm UTC](https://www.ruby-forum.com/t/how-to-deal-with-null-columns-when-displaying-db-tables/71140/6 "2006-10-05T17:51:35Z")

</div>

Thank you guys for your advices :).

Nicolas.

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [October 5, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/how-to-deal-with-null-columns-when-displaying-db-tables/71140/7 "2006-10-05T17:57:07Z")

</div>

On Oct 5, 11:42 am, “Chris M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [Law of Demeter - Wikipedia](http://en.wikipedia.org/wiki/Law_of_Demeter)
> 
> Chris

I like to do this instead of polluting my model with a bunch of virtual  
attributes…

\<%= h(person.department.name) rescue “None” %\>

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)
