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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 18 August 2011 21:58, Dorijan Jelincic <dmailj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.<%= 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Thu, Aug 18, 2011 at 2:51 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> 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 Grimm''s e-book on this topic well worth the price: <http://exceptionalruby.com/> -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> FWIW, I found Avdi Grimm''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"... -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.