Hey all,
I am not sure what to call this process, so I am having trouble looking 
up information on it.  I apologize if this is trivially easy.
I have a model "Metarecord" that maps to a table
"Metarecords" which has
a column "content_difficulty" (among others).  The
"content_difficulty"
is an integer from 1 (lowest difficulty) to 5 (highest difficulty).  I 
want calls to metarecord.content_difficulty to return the word/string 
representation instead of the integer.  This could be done either with a 
switch statement or using a lookup table in the DB.  That part seems 
ok.  I just don''t know how to get started redefining
.content_difficulty.
My java side would thinkup something like:
def content_difficulty
    return DifficultyString.find(super.content_difficulty)
end
Where DifficultyString is a model for the lookup table.  Tips?
Thanks,
Jeff
Hey Jeff-
     I have done something similar to this. In one of my apps I have  
project.status and project.priority feilds in the db as integers from  
1-5. I figured these are small enough options so they don''t need  
their own lookup table in the db. Here is how I worked it:
At the bottom of config/environment.rb I define 2 array constants:
PRIORITY = [''Not Set'', ''Highest'',
''High'', ''Medium'',
''Normal'', ''Low'']
STATUS = [''Not Set'', ''Just Opened'',
''In Progress'', ''On Hold'',
''Testing'', ''Finished'']
Then I can display the correct string value in a view by doing this:
Status: <%=  STATUS[project.status] %>
Priority:  <%= {PRIORITY[project.priority] %>
Hope that helps. It keeps it much simpler than a db lookup. And it  
still lets project.priority or project.status return the correct  
integer when you use them in a select dropdown. Because if you  
redefine your content_difficulty method to return just the string you  
might have problems updating the integer value stored in the db.
Cheers-
-Ezra
On Jul 27, 2005, at 3:25 PM, Jeff Casimir wrote:
> Hey all,
>
> I am not sure what to call this process, so I am having trouble  
> looking up information on it.  I apologize if this is trivially easy.
>
> I have a model "Metarecord" that maps to a table
"Metarecords"
> which has a column "content_difficulty" (among others).  The  
> "content_difficulty" is an integer from 1 (lowest difficulty) to
5
> (highest difficulty).  I want calls to  
> metarecord.content_difficulty to return the word/string  
> representation instead of the integer.  This could be done either  
> with a switch statement or using a lookup table in the DB.  That  
> part seems ok.  I just don''t know how to get started  
> redefining .content_difficulty.
>
> My java side would thinkup something like:
>
> def content_difficulty
>    return DifficultyString.find(super.content_difficulty)
> end
>
> Where DifficultyString is a model for the lookup table.  Tips?
>
> Thanks,
> Jeff
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
509-577-7732
ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org
On Jul 27, 2005, at 4:03 PM, Ezra Zygmuntowicz wrote:> Hey Jeff- > I have done something similar to this. In one of my apps I have > project.status and project.priority feilds in the db as integers > from 1-5. I figured these are small enough options so they don''t > need their own lookup table in the db. Here is how I worked it: > > At the bottom of config/environment.rb I define 2 array constants: > > PRIORITY = [''Not Set'', ''Highest'', ''High'', ''Medium'', ''Normal'', ''Low''] > STATUS = [''Not Set'', ''Just Opened'', ''In Progress'', ''On Hold'', > ''Testing'', ''Finished''] > > Then I can display the correct string value in a view by doing this: > Status: <%= STATUS[project.status] %> > Priority: <%= {PRIORITY[project.priority] %>^^^^^^^^^^^^^ this line should be Priority: <%= PRIORITY[project.priority] %> (typo)> > Hope that helps. It keeps it much simpler than a db lookup. And it > still lets project.priority or project.status return the correct > integer when you use them in a select dropdown. Because if you > redefine your content_difficulty method to return just the string > you might have problems updating the integer value stored in the db. > > Cheers- > -Ezra > > > On Jul 27, 2005, at 3:25 PM, Jeff Casimir wrote: > > >> Hey all, >> >> I am not sure what to call this process, so I am having trouble >> looking up information on it. I apologize if this is trivially easy. >> >> I have a model "Metarecord" that maps to a table "Metarecords" >> which has a column "content_difficulty" (among others). The >> "content_difficulty" is an integer from 1 (lowest difficulty) to 5 >> (highest difficulty). I want calls to >> metarecord.content_difficulty to return the word/string >> representation instead of the integer. This could be done either >> with a switch statement or using a lookup table in the DB. That >> part seems ok. I just don''t know how to get started >> redefining .content_difficulty. >> >> My java side would thinkup something like: >> >> def content_difficulty >> return DifficultyString.find(super.content_difficulty) >> end >> >> Where DifficultyString is a model for the lookup table. Tips? >> >> Thanks, >> Jeff >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > > -Ezra Zygmuntowicz > Yakima Herald-Republic > WebMaster > 509-577-7732 > ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-Ezra Zygmuntowicz Yakima Herald-Republic WebMaster 509-577-7732 ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org