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

**URL:** <https://discuss.rubyonrails.org/t/how-to-deal-with-null-columns-when-displaying-db-tables/2642>\
**Category:** rubyonrails-talk\
**Created:** [October 5, 2006, 2:13pm UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-columns-when-displaying-db-tables/2642 "2006-10-05T14:13:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maxim\_Kulkin](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@Maxim\_Kulkin](https://discuss.rubyonrails.org/u/Maxim_Kulkin)\
**Post date:** [October 5, 2006, 2:13pm UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-columns-when-displaying-db-tables/2642/1 "2006-10-05T14:13:43Z")

</div>

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:** ![Maxim\_Kulkin](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@Maxim\_Kulkin](https://discuss.rubyonrails.org/u/Maxim_Kulkin)\
**Post date:** [October 5, 2006, 3:01pm UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-columns-when-displaying-db-tables/2642/2 "2006-10-05T15:01:39Z")

</div>

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:** ![Chris\_Mear1](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@Chris\_Mear1](https://discuss.rubyonrails.org/u/Chris_Mear1)\
**Post date:** [October 5, 2006, 3:42pm UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-columns-when-displaying-db-tables/2642/3 "2006-10-05T15:42:44Z")

</div>

Nicolas Blanco wrote:

> For example I've got one line like :
> 
> \<td\>\<%= h(person.department.name) %\>\</td\>
> 
> 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 &nbsp;&nbsp;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:** ![olbrich](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/olbrich/32/10820_2.png) [@olbrich](https://discuss.rubyonrails.org/u/olbrich)\
**Post date:** [October 5, 2006, 3:54pm UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-columns-when-displaying-db-tables/2642/4 "2006-10-05T15:54:10Z")

</div>

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
