# Newbie: handling a method error

**URL:** <https://www.ruby-forum.com/t/newbie-handling-a-method-error/210860>\
**Category:** Rails\
**Created:** [August 18, 2011, 10:59pm UTC](https://www.ruby-forum.com/t/newbie-handling-a-method-error/210860 "2011-08-18T22:59:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [August 18, 2011, 10:59pm UTC](https://www.ruby-forum.com/t/newbie-handling-a-method-error/210860/1 "2011-08-18T22:59:52Z")

</div>

Hi…  
I have 2 tables, connected with belongs\_to and has\_one and everything  
works ok until I get one entry in base where I dont have set id of  
belongs\_to entry. How can I prevent it(except that that all entry has  
ids 🙂 or dont print anithing in view?

this is what I am talking about:  
I have customer and it has firm\_id  
trable firm has id and name

so when I try to print  
customer.firm.name I got error.  
Thank you

---

<div class="post-metadata">

**Author:** ![Dorijan\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e480ec/32.png) [@Dorijan\_J](https://www.ruby-forum.com/u/Dorijan_J)\
**Post date:** [August 18, 2011, 11:54pm UTC](https://www.ruby-forum.com/t/newbie-handling-a-method-error/210860/2 "2011-08-18T23:54:16Z")

</div>

On 18 August 2011 21:58, Dorijan J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> so when I try to print  
> customer.firm.name I got error.

\<%= customer.firm.name if customer.firm %\>

Or possibly better to provide a method, say firm\_name, of Customer  
that tests self.firm and returns the name or nil or maybe a default  
string if that is what you want, then do

\<%= customer.firm\_name %\>

Colin

---

<div class="post-metadata">

**Author:** ![Dorijan\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e480ec/32.png) [@Dorijan\_J](https://www.ruby-forum.com/u/Dorijan_J)\
**Post date:** [August 19, 2011, 12:07am UTC](https://www.ruby-forum.com/t/newbie-handling-a-method-error/210860/3 "2011-08-19T00:07:14Z")

</div>

On Thu, Aug 18, 2011 at 2:51 PM, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > customer.firm.name I got error.
> 
> \<%= customer.firm.name if customer.firm %\>
> 
> Or possibly better to provide a method, say firm\_name, of Customer  
> that tests self.firm and returns the name or nil or maybe a default  
> string if that is what you want, then do
> 
> \<%= customer.firm\_name %\>

FWIW, I found Avdi G.'s e-book on this topic well worth the price:

[http://exceptionalruby.com/](http://exceptionalruby.com/)

–  
Hassan S. ------------------------ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[Hassan Schroeder | about.me](http://about.me/hassanschroeder)  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![Dorijan\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e480ec/32.png) [@Dorijan\_J](https://www.ruby-forum.com/u/Dorijan_J)\
**Post date:** [August 19, 2011, 1:04pm UTC](https://www.ruby-forum.com/t/newbie-handling-a-method-error/210860/4 "2011-08-19T13:04:45Z")

</div>

> FWIW, I found Avdi G.'s e-book on this topic well worth the price:
> 
> [http://exceptionalruby.com/](http://exceptionalruby.com/)

thank you…I will look into to..  
I resolve the issue like this:  
in my controller, I asked if firm\_id==nil, and if it is nil, I  
assigned 0 value, witch is in my database described as “Not  
selected”…
