Nicolas Blanco
2006-Oct-05 13:59 UTC
how to deal with NULL columns when displaying db tables ?
Hi, I''m displaying a simple table from my model like : <% @people.each do |person| %> <tr> <td><%= h(person.email) %></td> <td><%= h(person.name) %> <td><%= h(person.adress) %> (...) </tr> <% end %> The problem is that some columns are sometimes NULL and I get an exception. I''ve put "if person..." in each line but i don''t like that way (it does not seem to be a "DRY" way). What could I do ? Thanks. Nicolas. -- 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 -~----------~----~----~----~------~----~------~--~---
Maxim Kulkin
2006-Oct-05 14:13 UTC
Re: how to deal with NULL columns when displaying db tables ?
On 5 October 2006 17:59, Nicolas Blanco wrote:> <% @people.each do |person| %> > > <tr> > <td><%= h(person.email) %></td> > <td><%= h(person.name) %> > <td><%= h(person.adress) %> > (...) > </tr> > > <% end %> > > The problem is that some columns are sometimes NULL and I get an > exception. > I''ve put "if person..." in each line but i don''t like that way (it does > not seem to be a "DRY" way). What could I do ?So, are you saying that you get an exception when you trying to get attribute value which is NULL ? That shouldn''t happen. Also, I''ve checked h() method and it should handle NULLs fine. Maybe you could provide exception error messages to examine ? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Nicolas Blanco
2006-Oct-05 14:38 UTC
Re: how to deal with NULL columns when displaying db tables
Maxim Kulkin wrote:> On 5 October 2006 17:59, Nicolas Blanco wrote: > >> >> The problem is that some columns are sometimes NULL and I get an >> exception. >> I''ve put "if person..." in each line but i don''t like that way (it does >> not seem to be a "DRY" way). What could I do ? > So, are you saying that you get an exception when you trying to get > attribute > value which is NULL ? That shouldn''t happen. > > Also, I''ve checked h() method and it should handle NULLs fine. > > Maybe you could provide exception error messages to examine ?OK. For example I''ve got one line like : <td><%= h(person.department.name) %></td> I get : "You have a nil object when you didn''t expect it! The error occured while evaluating nil.name" if i don''t test "person.department". I don''t understand. If I want to write <%= object.a.b.c.d %> and c/d = nil, writing <%= object.a.b.c.d if object.a.b.c.d %> won''t do it because nil.nil generates an exception... -- 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 -~----------~----~----~----~------~----~------~--~---
Maxim Kulkin
2006-Oct-05 15:01 UTC
Re: how to deal with NULL columns when displaying db tables
On 5 October 2006 18:38, Nicolas Blanco wrote:> For example I''ve got one line like : > > <td><%= h(person.department.name) %></td> > > I get : "You have a nil object when you didn''t expect it! > The error occured while evaluating nil.name" if i don''t test > "person.department".In this situation there is no other way to avoid error unless you are using Edge Rails where there is a patch that allow Nils to execute any unknown method with result of nil. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Chris Mear
2006-Oct-05 15:42 UTC
Re: how to deal with NULL columns when displaying db tables
Nicolas Blanco wrote:> For example I''ve got one line like : > > <td><%= h(person.department.name) %></td> > > I get : "You have a nil object when you didn''t expect it! > The error occured while evaluating nil.name" if i don''t test > "person.department".Yes, for this example, you just have to write: h(person.department.name) if person.department> I don''t understand. If I want to write <%= object.a.b.c.d %> and c/d > nil, writing <%= object.a.b.c.d if object.a.b.c.d %> won''t do it because > nil.nil generates an exception...This is exactly why it''s generally bad style to chain these kinds of calls too deeply, particularly in views. You end up having to do lots of this kind of checking, and it gets messy. That sort of knowledge is better kept in the model itself, rather than ''reaching through'' two or three objects, and having to deal with all the potential nils every time you do it. For instance, you could have this method in Person: def department_name self.department.name if self.department end And then it''s always safe to put this in your view: h(person.department_name) This principle is known as the Law of Demeter: http://en.wikipedia.org/wiki/Law_of_Demeter Chris --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Nicolas Blanco
2006-Oct-05 15:51 UTC
Re: how to deal with NULL columns when displaying db tables
Thank you guys for your advices :). Nicolas. -- 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 -~----------~----~----~----~------~----~------~--~---
On Oct 5, 11:42 am, "Chris Mear" <c...-OIzkuoyqg0kAvxtiuMwx3w@public.gmane.org> wrote:> Nicolas Blanco wrote: > > For example I''ve got one line like : > > > <td><%= h(person.department.name) %></td> > > > I get : "You have a nil object when you didn''t expect it! > > The error occured while evaluating nil.name" if i don''t test > > "person.department".Yes, for this example, you just have to write: > > h(person.department.name) if person.department > > > I don''t understand. If I want to write <%= object.a.b.c.d %> and c/d > > nil, writing <%= object.a.b.c.d if object.a.b.c.d %> won''t do it because > > nil.nil generates an exception...This is exactly why it''s generally bad style to chain these kinds of > calls too deeply, particularly in views. You end up having to do lots > of this kind of checking, and it gets messy. That sort of knowledge is > better kept in the model itself, rather than ''reaching through'' two or > three objects, and having to deal with all the potential nils every > time you do it. > > For instance, you could have this method in Person: > > def department_name > self.department.name if self.department > end > > And then it''s always safe to put this in your view: > > h(person.department_name) > > This principle is known as the Law of Demeter: > > http://en.wikipedia.org/wiki/Law_of_Demeter > > ChrisI like to do this instead of polluting my model with a bunch of virtual attributes.... <%= h(person.department.name) rescue "None" %> _Kevin www.sciwerks.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 -~----------~----~----~----~------~----~------~--~---