I nned to get an associsted name from one table dependent on the related id value in another table when rendering the view but cant figure out the syntax to handle pontential empty values:- i.e. docv.policy_id OR docv.standard_id will exist and therefore link back to the policies OR the standards table respectively but ONLY ONE will exist. Therefore I need to check the existance of each field before attempting to get the PolicyName OR StandrdsName from the related table otherwise the empty item gives me a ''.nil value'' error. <% @docversions.each do |docv| %> <tr> <td><% docv.modifier %></td> <td><%= docv.comment %></td> <td><%= docv.policy.PolicyName if docv.policy_id %></td> <td><%= docv.standard.StandardsName if docv.standard_id %></td> Can someone advise the syntax to use in the view to achieve this. I have also tried ....."if !docv.standard_id.empty?". Thanks -- 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 -~----------~----~----~----~------~----~------~--~---
On Dec 3, 1:59 pm, Martyn Elmy-liddiard <rails-mailing-l...@andreas- s.net> wrote:> > <% @docversions.each do |docv| %> > <tr> > <td><% docv.modifier %></td> > <td><%= docv.comment %></td> > <td><%= docv.policy.PolicyName if docv.policy_id %></td> > <td><%= docv.standard.StandardsName if docv.standard_id %></td> > > Can someone advise the syntax to use in the view to achieve this. I have > also tried ....."if !docv.standard_id.empty?". >don''t think you need anything more than if docv.policy Fred> Thanks > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On Dec 3, 1:59?pm, Martyn Elmy-liddiard <rails-mailing-l...@andreas- > s.net> wrote: >> > don''t think you need anything more than if docv.policy > > FredThanks Fred but I still get an error:- I gave a vey simple example and it may be fairer to give a slightly different example that I am working on where the potential nil value is the default selected value of a drop-down list:- <tr> <td><select name="risk[LastReviewed(1i)]"> <option value="<%= @risks.LastReviewed.year if @risks.LastReviewed.year %>" selected="selected"><%= @risks.LastReviewed.year %></option> <option value="2008">2008</option> .......etc and the error I get is:- You have a nil object when you didn''t expect it! The error occured while evaluating nil.year -- 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 -~----------~----~----~----~------~----~------~--~---
On Wed, Dec 3, 2008 at 4:30 PM, Martyn Elmy-liddiard < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Frederick Cheung wrote: > > On Dec 3, 1:59?pm, Martyn Elmy-liddiard <rails-mailing-l...@andreas- > > s.net> wrote: > >> > > don''t think you need anything more than if docv.policy > > > > Fred > > > Thanks Fred but I still get an error:- > > I gave a vey simple example and it may be fairer to give a slightly > different example that I am working on where the potential nil value is > the default selected value of a drop-down list:- > > <tr> > <td><select name="risk[LastReviewed(1i)]"> > <option value="<%= @risks.LastReviewed.year if @risks.LastReviewed.year > %>" selected="selected"><%= @risks.LastReviewed.year %></option> > <option value="2008">2008</option> > .......etc > > > and the error I get is:- > > You have a nil object when you didn''t expect it! > The error occured while evaluating nil.year > --That''s because @risks.LastReviewed is the one that is nil use <option value="<%= @risks.LastReviewed.year if @risks.LastReviewed%>" selected="selected">> > Posted via http://www.ruby-forum.com/. > > > >-- Mahmoud Said Software Developer blog.modsaid.com www.eSpace.com.eg +20-16-1223857 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
mahmoud said wrote:> On Wed, Dec 3, 2008 at 4:30 PM, Martyn Elmy-liddiard < > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> Thanks Fred but I still get an error:- >> .......etc >> >> >> and the error I get is:- >> >> You have a nil object when you didn''t expect it! >> The error occured while evaluating nil.year >> -- > > > That''s because @risks.LastReviewed is the one that is nil > use > <option value="<%= @risks.LastReviewed.year if @risks.LastReviewed%>" > selected="selected"> > > > -- > Mahmoud Said > Software Developer > blog.modsaid.com > www.eSpace.com.eg > +20-16-1223857Mahmoud, Still not quite seeing why @risks.LastReviewed can be checked for existance but @risks.LastReviewed.year cant be. I would have thought an element of the date would be able to be checked in the same way as the whole date. Anyhow...you help was great and has allowed me to sort the problem so thanks very much indeed. Just for anyone else....I also needed to put the existance check in the displayed element of the drop-down selection thus:- <option value="<%= @risks.LastReviewed.year if @risks.LastReviewed%>" selected="selected"><%= @risks.LastReviewed.year if @risks.LastReviewed%></option> Martyn -- 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 -~----------~----~----~----~------~----~------~--~---
On Wed, Dec 3, 2008 at 5:22 PM, Martyn Elmy-liddiard < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > mahmoud said wrote: > > On Wed, Dec 3, 2008 at 4:30 PM, Martyn Elmy-liddiard < > > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > >> Thanks Fred but I still get an error:- > >> .......etc > >> > >> > >> and the error I get is:- > >> > >> You have a nil object when you didn''t expect it! > >> The error occured while evaluating nil.year > >> -- > > > > > > That''s because @risks.LastReviewed is the one that is nil > > use > > <option value="<%= @risks.LastReviewed.year if @risks.LastReviewed%>" > > selected="selected"> > > > > > > -- > > Mahmoud Said > > Software Developer > > blog.modsaid.com > > www.eSpace.com.eg > > +20-16-1223857 > > Mahmoud, > > Still not quite seeing why @risks.LastReviewed can be checked for > existance but @risks.LastReviewed.year cant be. I would have thought an > element of the date would be able to be checked in the same way as the > whole date.In *@risks.LastReviewed.year* you are calling the *year* method of the * @risks.LastReviewed* object which, I assume, is of class *Time* . if that object is nil then it is no longer a Time object... it is a nil object of type NilClass <http://www.ruby-doc.org/core/classes/NilClass.html> .. and you can only call NilClass methods for it.> > > Anyhow...you help was great and has allowed me to sort the problem so > thanks very much indeed. > > Just for anyone else....I also needed to put the existance check in the > displayed element of the drop-down selection thus:- > > <option value="<%= @risks.LastReviewed.year if @risks.LastReviewed%>" > selected="selected"><%= @risks.LastReviewed.year if > @risks.LastReviewed%></option> > > Martyn > > -- > Posted via http://www.ruby-forum.com/. > > > >-- Mahmoud Said Software Developer blog.modsaid.com www.eSpace.com.eg +20-16-1223857 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---