# Ruby noob using array for databse lookups

**URL:** <https://www.ruby-forum.com/t/ruby-noob-using-array-for-databse-lookups/85390>\
**Category:** Rails\
**Created:** [February 13, 2007, 6:06am UTC](https://www.ruby-forum.com/t/ruby-noob-using-array-for-databse-lookups/85390 "2007-02-13T06:06:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![reed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@reed](https://www.ruby-forum.com/u/reed)\
**Post date:** [February 13, 2007, 6:06am UTC](https://www.ruby-forum.com/t/ruby-noob-using-array-for-databse-lookups/85390/1 "2007-02-13T06:06:12Z")

</div>

I have this model…

class Meeting \< ActiveRecord::Base  
MEETING\_TYPES = [[‘General’ , ‘g’],  
[‘Special’ , ‘s’],  
[‘AGM’ , ‘a’]]  
end

I have generated scaffolding and the edit and create are working as  
expected.  
However in the list it is not displaying the correct meeting type, it  
is displaying the value stored in the database (g,s,a) where I want  
“General”, “Special” or “AGM”.  
This is from my list.rhtml file…

\<% for meeting in @meetings %\>  
\<%=h meeting.send(:meeting\_type) %\>  
\<% end %\>

I can see why it is displaying the database values but I’m not sure  
how to retrieve the display values from the array or if there is some  
rails helper/magic I should be using.

I tried using a hash which solved this problem but broke the create/  
edit combobox.

---

<div class="post-metadata">

**Author:** ![reed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@reed](https://www.ruby-forum.com/u/reed)\
**Post date:** [February 13, 2007, 9:40am UTC](https://www.ruby-forum.com/t/ruby-noob-using-array-for-databse-lookups/85390/2 "2007-02-13T09:40:28Z")

</div>

On Feb 13, 7:05 am, “reed” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have this model…
> 
> class Meeting \< ActiveRecord::Base  
> MEETING\_TYPES = [[‘General’ , ‘g’],  
> [‘Special’ , ‘s’],  
> [‘AGM’ , ‘a’]]  
> end

Why not override the meeting\_type method?

def meeting\_type  
type = read\_attribute(‘meeting\_type’)  
tuple = MEETING\_TYPES.detect {|t| t[1] == type}  
tuple[0] unless tuple.nil?  
end

Note that meeting\_type= (the “setter”) is left alone so that it can  
still conveniently receive form values, where the database value would  
be used.

> \<% for meeting in @meetings %\>  
> \<%=h meeting.send(:meeting\_type) %\>  
> \<% end %\>

You’re overusing send(). Once you’ve overridden meeting\_type as shown  
above, you can just do this:

\<% for meeting in @meetings %\>  
\<%=h meeting.:meeting\_type %\>  
\<% end %\>

Ciao,  
Sheldon.

---

<div class="post-metadata">

**Author:** ![reed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@reed](https://www.ruby-forum.com/u/reed)\
**Post date:** [February 13, 2007, 12:36pm UTC](https://www.ruby-forum.com/t/ruby-noob-using-array-for-databse-lookups/85390/3 "2007-02-13T12:36:47Z")

</div>

Much appreciated Sheldon.

I guess the colon is a typo. - meeting.:meeting\_type

I used send because I am learning from the scaffold generator. It  
does read a lot clearer calling the method directly.

When should the send() method be used?

Cheers,  
Reed.

---

<div class="post-metadata">

**Author:** ![reed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@reed](https://www.ruby-forum.com/u/reed)\
**Post date:** [February 13, 2007, 12:52pm UTC](https://www.ruby-forum.com/t/ruby-noob-using-array-for-databse-lookups/85390/4 "2007-02-13T12:52:48Z")

</div>

On Feb 13, 1:35 pm, “reed” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> When should the send() method be used?

There are two situations in which send() makes sense:

1. When you write the code, you don’t know what method to call. It’s  
a runtime decision based on the contents of one or more variables.

2. You want to violate the method’s privacy, because you understand  
the risks and reckon it’s worth it in this particular situation.

Ciao,  
Sheldon.

---

<div class="post-metadata">

**Author:** ![reed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@reed](https://www.ruby-forum.com/u/reed)\
**Post date:** [February 14, 2007, 9:56pm UTC](https://www.ruby-forum.com/t/ruby-noob-using-array-for-databse-lookups/85390/5 "2007-02-14T21:56:41Z")

</div>

Here is what I did in the end…

def meeting\_type\_display  
tuple = MEETING\_TYPES.rassoc read\_attribute(‘meeting\_type’)  
tuple[0] if tuple  
end

---

<div class="post-metadata">

**Author:** ![reed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@reed](https://www.ruby-forum.com/u/reed)\
**Post date:** [February 13, 2007, 10:58pm UTC](https://www.ruby-forum.com/t/ruby-noob-using-array-for-databse-lookups/85390/6 "2007-02-13T22:58:02Z")

</div>

Overriding the meeting\_type caused a problem for the combobox - it  
wasn’t providing the correct value to the database so I named the  
method meeting\_type\_display and it works well.

Thanks for you help,

Reed
