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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 13, 7:05 am, "reed" <reed...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have this model... > > class Meeting < ActiveRecord::Base > MEETING_TYPES = [[''General'' , ''g''], > [''Special'' , ''s''], > [''AGM'' , ''a'']] > endWhy 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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, 9:39 pm, "Sheldon Hearn" <sheld...-Uqlk7TKVmMlWaveUIWBRJg@public.gmane.org> wrote:> On Feb 13, 7:05 am, "reed" <reed...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 13, 1:35 pm, "reed" <reed...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Feb 14, 12:51 am, "Sheldon Hearn" <sheld...-Uqlk7TKVmMlWaveUIWBRJg@public.gmane.org> wrote:> On Feb 13, 1:35 pm, "reed" <reed...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Feb 14, 10:57 am, "reed" <reed...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > On Feb 14, 12:51 am, "Sheldon Hearn" <sheld...-Uqlk7TKVmMlWaveUIWBRJg@public.gmane.org> wrote: > > > On Feb 13, 1:35 pm, "reed" <reed...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---