# Check for empty? in the view

**URL:** <https://www.ruby-forum.com/t/check-for-empty-in-the-view/154747>\
**Category:** Rails\
**Created:** [December 3, 2008, 2:59pm UTC](https://www.ruby-forum.com/t/check-for-empty-in-the-view/154747 "2008-12-03T14:59:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [December 3, 2008, 2:59pm UTC](https://www.ruby-forum.com/t/check-for-empty-in-the-view/154747/1 "2008-12-03T14:59:28Z")

</div>

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| %\>

\<% docv.modifier %\> \<%= docv.comment %\> \<%= docv.policy.PolicyName if docv.policy\_id %\> \<%= docv.standard.StandardsName if docv.standard\_id %\>

Can someone advise the syntax to use in the view to achieve this. I have  
also tried …“if !docv.standard\_id.empty?”.

Thanks

---

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [December 3, 2008, 3:06pm UTC](https://www.ruby-forum.com/t/check-for-empty-in-the-view/154747/2 "2008-12-03T15:06:14Z")

</div>

On Dec 3, 1:59 pm, Martyn Elmy-liddiard \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> 

don’t think you need anything more than if docv.policy

Fred

---

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [December 3, 2008, 3:30pm UTC](https://www.ruby-forum.com/t/check-for-empty-in-the-view/154747/3 "2008-12-03T15:30:14Z")

</div>

Frederick C. wrote:

> On Dec 3, 1:59?pm, Martyn Elmy-liddiard \<rails-mailing-l…@andreas-  
> [s.net](http://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:-

\<%= @risks.LastReviewed.year %\> 2008 .......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

---

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [December 3, 2008, 3:38pm UTC](https://www.ruby-forum.com/t/check-for-empty-in-the-view/154747/4 "2008-12-03T15:38:37Z")

</div>

On Wed, Dec 3, 2008 at 4:30 PM, Martyn Elmy-liddiard \<  
removed\_email\_address@domain.invalid\> 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

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Mahmoud Said  
Software Developer

> **[Thoughts](http://blog.modsaid.com)**

www.eSpace.com.eg  
+20-16-1223857

---

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [December 3, 2008, 4:22pm UTC](https://www.ruby-forum.com/t/check-for-empty-in-the-view/154747/5 "2008-12-03T16:22:35Z")

</div>

mahmoud said wrote:

> On Wed, Dec 3, 2008 at 4:30 PM, Martyn Elmy-liddiard \<  
> removed\_email\_address@domain.invalid\> 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
> 
> –  
> Mahmoud Said  
> Software Developer  
> [blog.modsaid.com](http://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.

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:-

\<%= @risks.LastReviewed.year if @risks.LastReviewed%\>

Martyn

---

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [December 3, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/check-for-empty-in-the-view/154747/6 "2008-12-03T16:30:20Z")

</div>

On Wed, Dec 3, 2008 at 5:22 PM, Martyn Elmy-liddiard \<  
removed\_email\_address@domain.invalid\> wrote:

> > >
> 
> 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](http://www.ruby-doc.org/core/classes/NilClass.html) …  
and  
you can only call NilClass methods for it.

> @risks.LastReviewed%\>
> 
> Martyn
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Mahmoud Said  
Software Developer

> **[Thoughts](http://blog.modsaid.com)**

www.eSpace.com.eg  
+20-16-1223857
