Ruby noob using array for databse lookups

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.

On Feb 13, 7:05 am, “reed” [email protected] 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.

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.

On Feb 13, 1:35 pm, “reed” [email protected] 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.

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

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