How to deal with NULL columns when displaying db tables?

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.

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 ?

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.

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:

http://en.wikipedia.org/wiki/Law_of_Demeter

Chris

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…

Thank you guys for your advices :).

Nicolas.

On Oct 5, 11:42 am, “Chris M.” [email protected] wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs