Hi! I am trying to switch from PHP to RoR. In my database, I have an integer represent a string. So, for example, a row would have a 1, 2, or 3. For example... 1 = Low 2 = Medium 3 = High I do not have the Low/Med/High anywhere in the database; I just know that in a row, 1 is low, 2 is medium, etc... In my view, when I "show" a record, the record is shown as ''1'' when I use the scaffolding. In PHP, I would just implement a ''case'' statement depending on what the number represents. I was wondering how I would do it in RoR? Would I use a if statement in the view? Put something in the controller? Thanks... --~--~---------~--~----~------------~-------~--~----~ 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 Jul 9, 5:06 pm, "wiz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <wiz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi! > > I am trying to switch from PHP to RoR. In my database, I have an > integer represent a string. So, for example, a row would have a 1, 2, > or 3. For example... > > 1 = Low > 2 = Medium > 3 = High > > I do not have the Low/Med/High anywhere in the database; I just know > that in a row, 1 is low, 2 is medium, etc... In my view, when I > "show" a record, the record is shown as ''1'' when I use the > scaffolding. > > In PHP, I would just implement a ''case'' statement depending on what > the number represents. I was wondering how I would do it in RoR? > Would I use a if statement in the view? Put something in the > controller? > > Thanks...Probably better to put it in the model (untested code :-): class MyModel < ActiveRecord:base def priority case priority_column_name # whatever column you''ve used to hold the integer when 1 return ''High'' when 2 return ''Medium'' when 3 return ''Low'' end ''Unknown priority'' end and then in the view call: <h2> Priority: <%= @my_model_instance.priority %> </h2> Allan --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
How about a Helper? On Jul 9, 2007, at 12:06 PM, wiz561-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > Hi! > > I am trying to switch from PHP to RoR. In my database, I have an > integer represent a string. So, for example, a row would have a 1, 2, > or 3. For example... > > 1 = Low > 2 = Medium > 3 = High > > I do not have the Low/Med/High anywhere in the database; I just know > that in a row, 1 is low, 2 is medium, etc... In my view, when I > "show" a record, the record is shown as ''1'' when I use the > scaffolding. > > In PHP, I would just implement a ''case'' statement depending on what > the number represents. I was wondering how I would do it in RoR? > Would I use a if statement in the view? Put something in the > controller? > > Thanks... > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thank you for the information. I''m currently trying to look at what a helper is and how to implement it. :) I think that''s the correct route that should be taken now. However, if I get too frustrated, I''ll probably use something like Allan suggested because I think that would work as well! Thanks again for the fast response! On Jul 9, 11:18 am, Anthony Carlos <anth...-icZdIMMiY0Opv2MiSiIzJgC/G2K4zDHf@public.gmane.org> wrote:> How about a Helper? > > On Jul 9, 2007, at 12:06 PM, wiz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > > > > Hi! > > > I am trying to switch from PHP to RoR. In my database, I have an > > integer represent a string. So, for example, a row would have a 1, 2, > > or 3. For example... > > > 1 = Low > > 2 = Medium > > 3 = High > > > I do not have the Low/Med/High anywhere in the database; I just know > > that in a row, 1 is low, 2 is medium, etc... In my view, when I > > "show" a record, the record is shown as ''1'' when I use the > > scaffolding. > > > In PHP, I would just implement a ''case'' statement depending on what > > the number represents. I was wondering how I would do it in RoR? > > Would I use a if statement in the view? Put something in the > > controller? > > > Thanks...--~--~---------~--~----~------------~-------~--~----~ 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 Mon, 2007-07-09 at 09:06 -0700, wiz561-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Hi! > > I am trying to switch from PHP to RoR. In my database, I have an > integer represent a string. So, for example, a row would have a 1, 2, > or 3. For example... > > 1 = Low > 2 = Medium > 3 = High > > I do not have the Low/Med/High anywhere in the database; I just know > that in a row, 1 is low, 2 is medium, etc... In my view, when I > "show" a record, the record is shown as ''1'' when I use the > scaffolding. > > In PHP, I would just implement a ''case'' statement depending on what > the number represents. I was wondering how I would do it in RoR? > Would I use a if statement in the view? Put something in the > controller? > > Thanks...I would map the numbers in the model: class MyModel PRIORITIES = { 1 => ''Low'', 2 => ''Medium'', 3 => ''High'' } end --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> In PHP, I would just implement a ''case'' statement depending on what > the number represents. I was wondering how I would do it in RoR? > Would I use a if statement in the view? Put something in the > controller?I would add a method to your model. Say the db field is "priority" ... def priority_name case priority when 1 "Low" when 2 "Medium" when 3 "High" else "Oops." end end Then, call @model.priority_name in your view instead of @model.priority. The advantage of putting it in the model instead of views is that if you add another priority, it''s easy to change in 1 place. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Oh my god....that really worked! Thank you so very much!!!!!! As I said, I''m just starting out with RoR and I guess I''m just surprised that it worked without many problems! Thank you again so much!!!! Mike On Jul 9, 11:26 am, "Michael D. Ivey" <michael.i...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > In PHP, I would just implement a ''case'' statement depending on what > > the number represents. I was wondering how I would do it in RoR? > > Would I use a if statement in the view? Put something in the > > controller? > > I would add a method to your model. > > Say the db field is "priority" ... > > def priority_name > case priority > when 1 > "Low" > when 2 > "Medium" > when 3 > "High" > else > "Oops." > end > end > > Then, call @model.priority_name in your view instead of @model.priority. > > The advantage of putting it in the model instead of views is that if > you add another priority, it''s easy to change in 1 place.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
wiz561 wrote:> Oh my god....that really worked! Thank you so very much!!!!!!>> def priority_namereturn {1=>"Low",2=>"Medium",3=>High}.fetch(priority, "Oops")>> endSometimes one kind of statement works better and sometimes another works better.>> The advantage of putting it in the model instead of views is that if >> you add another priority, it''s easy to change in 1 place.Google "Don''t Repeat Yourself". It''s a deceptively simple concept that motivates all of software design. -- Phlip http://www.oreilly.com/catalog/9780596510657/ "Test Driven Ajax (on Rails)" assert_xpath, assert_javascript, & assert_ajax --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---