# How can determine if an item has a value or not?

**URL:** <https://www.ruby-forum.com/t/how-can-determine-if-an-item-has-a-value-or-not/143926>\
**Category:** Rails\
**Created:** [July 23, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/how-can-determine-if-an-item-has-a-value-or-not/143926 "2008-07-23T22:30:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![monsterag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@monsterag](https://www.ruby-forum.com/u/monsterag)\
**Post date:** [July 23, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/how-can-determine-if-an-item-has-a-value-or-not/143926/1 "2008-07-23T22:30:12Z")

</div>

When i execute the followin code i get the following message:  
“comparison of Time with String failed”

\<% @auftrag.attributes.each do |key, item| %\>

\<%if item \> “” %\>  
**Key: \<%= key %\>:**  
Item: \<%= item %\>  
\<%end%\>

\<%end%\>

One of the fields in my database is a datefield and seems not to work  
with \> “”  
How can determine if an item is filled or not?

tia

---

<div class="post-metadata">

**Author:** ![monsterag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@monsterag](https://www.ruby-forum.com/u/monsterag)\
**Post date:** [July 23, 2008, 10:55pm UTC](https://www.ruby-forum.com/t/how-can-determine-if-an-item-has-a-value-or-not/143926/2 "2008-07-23T22:55:32Z")

</div>

Hi Einar,

Einar K. wrote:

> How can determine if an item is filled or not?

\<% unless item.nil? %\>

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![monsterag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@monsterag](https://www.ruby-forum.com/u/monsterag)\
**Post date:** [July 23, 2008, 11:13pm UTC](https://www.ruby-forum.com/t/how-can-determine-if-an-item-has-a-value-or-not/143926/3 "2008-07-23T23:13:33Z")

</div>

Bill W. wrote:

> Hi Einar,
> 
> Einar K. wrote:
> 
> > How can determine if an item is filled or not?
> 
> \<% unless item.nil? %\>
> 
> HTH,  
> Bill  
> Thanks, but there are still the empty key – items shown

\<% @auftrag.attributes.each do |key, item| %\>  
\<% unless item.nil? %\>  
**Key: \<%= key %\>**  
\<%= item %\>  
\<%end%\>  
\<%end%\>

produces something like:  
Key: sonst\_verm Key: gemeinschaft Key: whg\_verm Key: updated\_at Tue Jul  
22 16:23:44 +0200 2008 Key: hs\_verm Key: format werwer Key: etw\_vk Key:  
anzeigentext lkjl Key: fioport ljkkjhj Key: art jklhjlk Key: immo\_sonst  
Key: gesuch\_immo Key: dateien Key: zeitung\_sonst Key: id 4 Key: zeitung  
newspapername Key: immo\_vk Key: ang\_gewerbe Key: sonder\_hoehe Key:  
sonder\_breite Key: ersch\_datum 2008-07-21 Key: spalten 8 Key: created\_at  
Mon Jul 21 21:04:40 +0200 2008

i want to get rid of the empty key --values pairs where there are no  
entries in the database

---

<div class="post-metadata">

**Author:** ![monsterag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@monsterag](https://www.ruby-forum.com/u/monsterag)\
**Post date:** [July 23, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/how-can-determine-if-an-item-has-a-value-or-not/143926/4 "2008-07-23T23:41:46Z")

</div>

Try

\<% unless item.empty? %\>

Regards  
Kieran

On Thu, Jul 24, 2008 at 9:13 AM, Einar K. \<

---

<div class="post-metadata">

**Author:** ![monsterag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@monsterag](https://www.ruby-forum.com/u/monsterag)\
**Post date:** [July 23, 2008, 11:44pm UTC](https://www.ruby-forum.com/t/how-can-determine-if-an-item-has-a-value-or-not/143926/5 "2008-07-23T23:44:08Z")

</div>

Einar K. wrote:

> > HTH,
> 
> where there are no entries in the database  
> Sorry about that. Changing item.nil? to item.blank? should take care of  
> the  
> other items. blank? returns true for both nil and an empty string.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![monsterag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@monsterag](https://www.ruby-forum.com/u/monsterag)\
**Post date:** [July 23, 2008, 11:47pm UTC](https://www.ruby-forum.com/t/how-can-determine-if-an-item-has-a-value-or-not/143926/6 "2008-07-23T23:47:49Z")

</div>

On Wed, Jul 23, 2008 at 3:30 PM, Einar K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<%end%\>
> 
> One of the fields in my database is a datefield and seems not to work  
> with \> “”  
> How can determine if an item is filled or not?

Use either item.nil? or item.blank?

---

<div class="post-metadata">

**Author:** ![monsterag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@monsterag](https://www.ruby-forum.com/u/monsterag)\
**Post date:** [July 24, 2008, 8:34am UTC](https://www.ruby-forum.com/t/how-can-determine-if-an-item-has-a-value-or-not/143926/7 "2008-07-24T08:34:46Z")

</div>

thanks item.blank has made the trick 🙂
