Forum: Ruby on Rails Ruby noob using array for databse lookups

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
reed (Guest)
on 2007-02-13 07:06
(Received via mailing list)
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.
Sheldon H. (Guest)
on 2007-02-13 10:40
(Received via mailing list)
On Feb 13, 7:05 am, "reed" <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.
reed (Guest)
on 2007-02-13 13:36
(Received via mailing list)
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.
Sheldon H. (Guest)
on 2007-02-13 13:52
(Received via mailing list)
On Feb 13, 1:35 pm, "reed" <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.
reed (Guest)
on 2007-02-13 23:58
(Received via mailing list)
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
reed (Guest)
on 2007-02-14 22:56
(Received via mailing list)
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
This topic is locked and can not be replied to.