Newbie: handling a method error

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

On 18 August 2011 21:58, Dorijan J. [email protected] 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

On Thu, Aug 18, 2011 at 2:51 PM, Colin L. [email protected]
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/


Hassan S. ------------------------ [email protected]

twitter: @hassan

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

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”…