# Nil result on find & pretty print

**URL:** <https://www.ruby-forum.com/t/nil-result-on-find-pretty-print/55230>\
**Category:** Rails\
**Created:** [March 18, 2006, 8:41am UTC](https://www.ruby-forum.com/t/nil-result-on-find-pretty-print/55230 "2006-03-18T08:41:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steven\_Heimann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@Steven\_Heimann](https://www.ruby-forum.com/u/Steven_Heimann)\
**Post date:** [March 18, 2006, 8:41am UTC](https://www.ruby-forum.com/t/nil-result-on-find-pretty-print/55230/1 "2006-03-18T08:41:56Z")

</div>

I am new to Ruby and to Ruby on Rails and have spent the last few days  
trying to get my head around it. I think I am already hooked but am  
suffering badly from years of programming a non-OO RDBMS. I am trying  
to learn by attempting to produce a system for my work.

I have a main table sdocs which has a field supplier\_id which is an  
entry in another table (suppliers) to lookup supplier name etc. At the  
moment I am not using the id field of suppliers in an attempt to get  
around the following error. (The agile development book suggests that  
the nil error only results from a search on a key and not from a search  
on other fields but I am still having the problem.)

“You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.suppname”

Sorry the code is such a mess at the moment because I have tried many  
different possible solutions.

Part of the code follows:

\<% require ‘pp’ %\>  
\<% for sdoc in @sdocs %\>

 ( \<%= pp Sdoc.columns.map { |col| col.name } %\> ) \<%= sdoc.supplier\_id %\>  
 \<% supp = Supplier.find(:first, :conditions =\> ["suppcode = ?" , sdoc.supplier\_id]) %\> \<%= supp.suppname %\> \<%= sdoc.supdesc %\> **/**  
 \<%= sdoc.rm\_id %\> \<%= sdoc.docdate %\>  
\<%= sdoc.expdate %\> \<%= sdoc.doctype.docdesc %\> \<%= link\_to 'Show', :action =\> 'show', :id =\> sdoc %\>  
 \<%= link\_to 'Edit', :action =\> 'edit', :id =\> sdoc %\>  
 \<%= link\_to 'Destroy', { :action =\> 'destroy', :id =\> sdoc }, :confirm =\> 'Are you sure?' %\> \<% end %\>

Where it tries to display supp.suppname I originally had  
sdoc.supplier.suppname. This was when I had a link  
using foreign keys. This works if the foreign key is always present and  
always correct but this is not the case with this database.

I have googled for this problem but the suggested solutions do not seem  
to work. Things like the following have not helped.

\<% if sdoc.supplier.nil %\>  
“test”  
\<% else %\>  
\<%= sdoc.supplier.suppname %\>  
\<% end %\>

I am sure this is all because of my ignorance but I haven’t been able to  
find a solution.

As part of debugging I tried pp as above. This is a line straight out  
of the agile development book but produces nothing.

Thank you for your help  
Steven

---

<div class="post-metadata">

**Author:** ![Steven\_Heimann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@Steven\_Heimann](https://www.ruby-forum.com/u/Steven_Heimann)\
**Post date:** [March 18, 2006, 9:09am UTC](https://www.ruby-forum.com/t/nil-result-on-find-pretty-print/55230/2 "2006-03-18T09:09:09Z")

</div>

Hi Steven

\<% supp = Supplier.find(:first, :conditions =\> [ “suppcode = ?” ,  
sdoc.supplier\_id]) %\>

After above line, supp will be nil if no suppliers satisfy the search  
conditions so just do this

\<%= supp.suppname unless supp.nil? %\>

or you can add a to\_s method to your supplier model like so

def to\_s  
suppname  
end

then you can use \<%= supp.to\_s %\> … which doesn’t need a conditional  
because nil.to\_s evaluates to ‘’

Steven Beales

“Steven Heimann” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in  
message news:removed\_email\_address@domain.invalid…

---

<div class="post-metadata">

**Author:** ![Steven\_Heimann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@Steven\_Heimann](https://www.ruby-forum.com/u/Steven_Heimann)\
**Post date:** [March 18, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/nil-result-on-find-pretty-print/55230/3 "2006-03-18T19:05:24Z")

</div>

> > \<%= supp.suppname unless supp.nil? %\>

should work, but:

> > \<%= supp.suppname rescue nil%\>

will print nil ( ‘’ )  
I posted an article on www.rails.co.za  
 about it the other day…

Anyway,  
Gustav P.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Steven\_Heimann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@Steven\_Heimann](https://www.ruby-forum.com/u/Steven_Heimann)\
**Post date:** [March 18, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/nil-result-on-find-pretty-print/55230/4 "2006-03-18T19:06:29Z")

</div>

Woops! Didn’t know R-Forum would escape the anchor . . . sorry :]
