miravol-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Oct-21 20:19 UTC
How to render 1="User" 2="Moderator" 3="Admin" etc.
Am in the midst of creating a largish app & have suddenly got struck with brain freeze. I want to store an integral value in table, but render it in HTML as text. So if the value is 1 I''d display the word "User" in a listbox; if it''s 2 I''d display it as "Moderator", etc. That way later if I want to change the word "Moderator" to "Helper" or whatever the numeric representation would not change, but the way it appears would. Can someone at the very least a) tell me what it''s called when one wants to render an enumerated value as something other than the number so I can look it up, or b) point me at some sample code or even API docs? Because I can''t think of what this is called I don''t know what to search for! One too many all-nighters... TIA --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
William Pratt
2007-Oct-21 20:31 UTC
Re: How to render 1="User" 2="Moderator" 3="Admin" etc.
One way I do it is to override the attribute accessor to return the enumerated value: in your model (assuming the column is called usertype): @@usertypes = %w[User Moderator] def usertype @@usertypes[self[:usertype].to_i] end There may be other ways, but this works well for me. miravol-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Am in the midst of creating a largish app & have suddenly got struck > with brain freeze. I want to store an integral value in table, but > render it in HTML as text. So if the value is 1 I''d display the word > "User" in a listbox; if it''s 2 I''d display it as "Moderator", etc. > That way later if I want to change the word "Moderator" to "Helper" or > whatever the numeric representation would not change, but the way it > appears would. > > Can someone at the very least a) tell me what it''s called when one > wants to render an enumerated value as something other than the number > so I can look it up, or b) point me at some sample code or even API > docs? Because I can''t think of what this is called I don''t know what > to search for! One too many all-nighters... > > TIA > > > > >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---