how to deal with NULL columns when displaying db tables ?

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 ?

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.

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   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:

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