Hello. I am able to get my setup working within the Rails console, but not within Rails. memberships ==========id (PK) ... dividends ========id (PK) membership_id (FK) ... Note: I originally had memberships_id, but this did not work correctly. membership.rb has_one :dividend dividend.rb belongs_to :membership I am trying to loop through the memberships table, printing a mix of information from this table and from the dividends table. My console session below shows that I have the relationships setup properly. # ruby script/console # >> member = Membership.find(1) # >> member.first_name "Michael" #>> member.dividend.broker_name "E-Trade" Great, now for the portion of my view in rails that is causing the error. <td bgcolor="#EEEEEE"><%= membership.id %></td> <td bgcolor="#EEEEEE"><%= membership.first_name %></td> <td bgcolor="#EEEEEE"><%= membership.dividend.broker_name || "Not Entered" %></td> If I remove the third line (membership.dividend.broker_name), it will find anything in the memberships table, but crashes if I try to display anything from the dividend table. The log file identifies the specific query that is causing a problem. There is a record in the memberships table (probably quite a few) that DO NOT have a corresponding record in the dividend table. I figured the ( || "Not Entered") would suffice, but I''m still getting a nasty "NoMethodError". You have a nil object when you didn''t expect it! The error occurred while evaluating nil.broker_name Do I have this setup correctly? Can active record handle the relationship if corresponding rows don''t exist in the child table? Thanks for reading and any possible suggestions toward resolution. -Brenden -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Do I have this setup correctly? Can active record handle the > relationship if corresponding rows don''t exist in the child table?You have got the relationship set up correctly. However, member.dividend will only return the associated dividend object if one exists. If one does not exist, it will return nil. Therefore, you get the error because you''re trying to call ''broker_name'' on a nil object. Basically, you need to add a bit more logic: <% if membership.dividend %> <%= member.dividend.broker_name %> <% else %> Not Entered <% end %> There is probably a way to make this a bit neater for your views, but hopefully this''ll help you on your way. Steve --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Basically, you need to add a bit more logic: > > <% if membership.dividend %> > <%= member.dividend.broker_name %> > <% else %> > Not Entered > <% end %> >Thanks Steve. Your suggestion worked. Much appreciated. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Stephen Bartholomew wrote:> There is probably a way to make this a bit neater for your views, but > hopefully this''ll help you on your way.you could always use the "?" operator: <%= membership.dividend ? member.dividend.broker_name : "Not Entered" %>> Steveregards, Rolando.- -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---