Nil column results, possible to ignore?


#1

Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


#2

On 1/8/06, Dylan M. 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 %>


#3

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


#4

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


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


#5

On 1/8/06, Jeremy E. removed_email_address@domain.invalid wrote:

Rails mailing list
removed_email_address@domain.invalid
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


#6

Just food for though… :slight_smile:
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…


#7

Enrico T. wrote:

wasn’t it? Do you think is it possible?
Just food for though… :slight_smile:

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