Hi, I''ve got a question regarding my models in my Rails app. Let''s say I have an object: User which has_many: Thing1 and Thing1 has_many: Thing2 and Thing2 has_one: Thing3 When I go to display this in my template, like this: <table border="1"> <tr> <th>Title</th> <th>Date</th> <th>Event</th> </tr> <% @user.thing1.each do |c| %> <tr> <td><%=h c.title %></td> <td><%=h c.date %></td> <td> <table> <tr> <th>Thing2</th> </tr> <% c.thing2.each do |be| %> <tr> <td> <%=h be.workout_type %></td> <%=h be.thing3.name %> </td> </tr> <% end %> </table> </td> </tr> <% end %> I get: undefined method `name'' for nil:NilClass I''m assuming this means there is no Thing3 object for this particular Thing2 object. I just bought Agile Development with Rails and am starting to go through that...it''s surely noob question but can someone set me straight? Thanks! Jon -- 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 7 March 2010 00:26, ObjectEvolution <objectevolution-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> <% c.thing2.each do |be| %> > <tr> > <td> > <%=h be.workout_type %></td> > <%=h be.thing3.name %> > </td> > </tr> > <% end %> > > I get: > undefined method `name'' for nil:NilClass > > I''m assuming this means there is no Thing3 object for this particular > Thing2 object.you''re quite correct - there''s no "thing3" linked to a given "thing 2". If there *supposed* to be an associated object there, then something''s gone wrong when you''ve created your objects, but if it''s a case that there may or may not be a Thing3, then a simple "guard" would suffice. <%=h be.thing3.name if be.thing3 %> -- 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.
Perfect...thanks! On Mar 7, 4:02 am, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 7 March 2010 00:26, ObjectEvolution <objectevolut...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > <% c.thing2.each do |be| %> > > <tr> > > <td> > > <%=h be.workout_type %></td> > > <%=h be.thing3.name %> > > </td> > > </tr> > > <% end %> > > > I get: > > undefined method `name'' for nil:NilClass > > > I''m assuming this means there is no Thing3 object for this particular > > Thing2 object. > > you''re quite correct - there''s no "thing3" linked to a given "thing 2". > If there *supposed* to be an associated object there, then something''s > gone wrong when you''ve created your objects, but if it''s a case that > there may or may not be a Thing3, then a simple "guard" would suffice. > > <%=h be.thing3.name if be.thing3 %>-- 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.