# Accessing the keyname and the value

**URL:** <https://www.ruby-forum.com/t/accessing-the-keyname-and-the-value/143889>\
**Category:** Rails\
**Created:** [July 23, 2008, 4:00pm UTC](https://www.ruby-forum.com/t/accessing-the-keyname-and-the-value/143889 "2008-07-23T16:00:20Z")\
**Posts on this page:** 4\
**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, 4:00pm UTC](https://www.ruby-forum.com/t/accessing-the-keyname-and-the-value/143889/1 "2008-07-23T16:00:20Z")

</div>

Hello  
in PHP i can access the Keynames of an array by using this option:

```
    while(list($k,$v) = each($uebergabe)) {
                    if($k <> "Abschicken" ){
                            $koepfe[] = $k;
                            $werte[] = $v;
                    }//if
            }//while

```

I want do use this in rails do make my code more flexible. I have a  
database and in the showview it is displayed like this:

**Zeitung:** \<%=h @auftrag.zeitung %\>

**Zeitung sonst:** \<%=h @auftrag.zeitung\_sonst %\>

**Gemeinschaft:** \<%=h @auftrag.gemeinschaft %\>

**Ersch datum:** \<%=h @auftrag.ersch\_datum %\>

**Art:** \<%=h @auftrag.art %\>

**Spalten:** \<%=h @auftrag.spalten %\>

**Format:** \<%=h @auftrag.format %\>

But if a field ist empty the name is also displayed.  
so my thougt ist something like this:  
\<% @auftrag.each do |item|%\>  
\<% if @auftrag.format \> “” %\>  
**\<%= KEYNAME %\>:**  
\<%= h @auftrag.format %\>  
\<%end%\>  
\<%end%\>

BUT how do i get the name of the current key belonging to the value?  
The Array @auftrag is defined in the Controller by using  
@auftrags = Auftrag.find(:all)

Any Idea?

---

<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, 5:42pm UTC](https://www.ruby-forum.com/t/accessing-the-keyname-and-the-value/143889/2 "2008-07-23T17:42:00Z")

</div>

\<% @auftrag.attributes.each do |key, item| %\>  
\<% if @auftrag.format \> “” %\>  
**\<%= key %\>:**  
\<%= h @auftrag.format %\>  
\<%end%\>  
\<%end%\>

would work

btw: using german for naming is a bad idea,  
since it breaks Rails pluralization.

---

<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, 9:05pm UTC](https://www.ruby-forum.com/t/accessing-the-keyname-and-the-value/143889/3 "2008-07-23T21:05:24Z")

</div>

Thanks Thorsten that helped a lot.  
Simple solution 🙂

---

<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, 9:27pm UTC](https://www.ruby-forum.com/t/accessing-the-keyname-and-the-value/143889/4 "2008-07-23T21:27:23Z")

</div>

next problem: when i compare the items with “”  
I get the follwing error, when i have a DATE-Field:  
“comparison of Time with String failed”  
Is there a more elegant way to ask if the Item-Field has values in it?
