My question is with regards to the field type that should be used for fields like status or priority. Should you use: 1.) A varchar and store the actual status eg. "open", "closed" 2.) Use an field type of int and store an number which can be display as a status using a helper method 3.) Use a field type of int and use a table which maps an id to a status If someone has a link to an example app that would be great! Thanks a lot, K.
On Fri, 2005-09-30 at 19:16 +0100, Kris Leech wrote: kris,> Should you use: > > 1.) A varchar and store the actual status eg. "open", "closed" > 2.) Use an field type of int and store an number which can be display as > a status using a helper method > 3.) Use a field type of int and use a table which maps an id to a status2.) Has the definite advantage in that in your view you can use the checkbox helper, like so: <%= check_box ''status'', ''closed'' %> which will set your the closed attribute of your status model to 0 if the box is not checked, or 1 if it is HTH, Howard
Thanks Howard, but my status field is likely to have several status''s, I should have made that clearer. I was thinking a combo box would work well. Therefore if I use a int field type to store the status how do I convert the number to a text string to display in a view (using a helper method?) and how do I display a combo box to allow a user to choose a status which can then be sent to the create or edit method as the appropriate number? Thanks, K. Howard Roberts wrote:>On Fri, 2005-09-30 at 19:16 +0100, Kris Leech wrote: >kris, > > >>Should you use: >> >>1.) A varchar and store the actual status eg. "open", "closed" >>2.) Use an field type of int and store an number which can be display as >>a status using a helper method >>3.) Use a field type of int and use a table which maps an id to a status >> >> >2.) Has the definite advantage in that in your view you can use the checkbox helper, like so: ><%= check_box ''status'', ''closed'' %> >which will set your the closed attribute of your status model to 0 if >the box is not checked, or 1 if it is > >HTH, >Howard > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-- Interkonect Services UK Ltd. Boundary House Main Street Hoveringham Nottingham NG147 JR web: www.interkonect.com tel: 0115 9663696 fax: 0115 9663696
Hey Kris- You could do something like this: First lets say you need a priority field to be a combo box on display but an int filed in the db: Define this array in the bottom of your environment.rb: PRIORITY = [''High'', ''Medium'', ''Low'', ''Very Low''] Now you have your field in the db as an int (1) and you store the numbers 0-3 in it. Then you could build a combo box like so. <select name="priority"> <option value="0">PRIORITY[0]</option> <option value="1">PRIORITY[1]</option> <option value="2">PRIORITY[2]</option> <option value="3">PRIORITY[3]</option> </select> Hope that helps some. Then whenever you want to display the priority that was already set, say in your list view just do: Priority: <%= PRIORITY[@object.priority] %> -Ezra On Oct 1, 2005, at 2:32 AM, Kris Leech wrote:> Thanks Howard, but my status field is likely to have several > status''s, I should have made that clearer. I was thinking a combo > box would work well. > > Therefore if I use a int field type to store the status how do I > convert the number to a text string to display in a view (using a > helper method?) and how do I display a combo box to allow a user to > choose a status which can then be sent to the create or edit method > as the appropriate number? > > Thanks, K. > > Howard Roberts wrote: > > >> On Fri, 2005-09-30 at 19:16 +0100, Kris Leech wrote: >> kris, >> >>> Should you use: >>> >>> 1.) A varchar and store the actual status eg. "open", "closed" >>> 2.) Use an field type of int and store an number which can be >>> display as a status using a helper method >>> 3.) Use a field type of int and use a table which maps an id to a >>> status >>> >>> >> 2.) Has the definite advantage in that in your view you can use >> the checkbox helper, like so: >> <%= check_box ''status'', ''closed'' %> >> which will set your the closed attribute of your status model to 0 if >> the box is not checked, or 1 if it is >> >> HTH, >> Howard >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> >> > > > -- > Interkonect Services UK Ltd. > Boundary House Main Street > Hoveringham > Nottingham > NG147 JR > > web: www.interkonect.com > tel: 0115 9663696 > fax: 0115 9663696 > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org 509-577-7732
Thanks Ezra, that works great :) I like this method but I have two problems the first is how to construct the conditions for a find so that tasks with a certain priority, say ''deleted'' are not shown. If I knew the number for the priority I wanted to filter on then that would be fine, but what if I change my order in the array at a later date. Really I need to specify a string eg "deleted" and have it converted to the number for use in a find. @tasks = Task.find(:all, :conditions => ["priority != ?", ???]) The second problem is when displaying the combo box how do I have the correct option selected if a status already exists (eg. editing a record). <select id="task_status" name="task[status]"> <option value="0"><%= TASK_STATUS[0] %></option> <option value="1"><%= TASK_STATUS[1] %></option> <option value="2"><%= TASK_STATUS[2] %></option> <option value="3"><%= TASK_STATUS[3] %></option> <option value="4"><%= TASK_STATUS[4] %></option> </select> Would I insert something like: <option value="0" <%= if (@task.priority = 0) then puts " ''selected''" end %>><%= TASK_STATUS[0] %></option> Ezra Zygmuntowicz wrote:> Hey Kris- > You could do something like this: First lets say you need a > priority field to be a combo box on display but an int filed in the db: > Define this array in the bottom of your environment.rb: > > PRIORITY = [''High'', ''Medium'', ''Low'', ''Very Low''] > > Now you have your field in the db as an int (1) and you store the > numbers 0-3 in it. Then you could build a combo box like so. > > <select name="priority"> > <option value="0">PRIORITY[0]</option> > <option value="1">PRIORITY[1]</option> > <option value="2">PRIORITY[2]</option> > <option value="3">PRIORITY[3]</option> > </select> > > Hope that helps some. Then whenever you want to display the priority > that was already set, say in your list view just do: > > Priority: <%= PRIORITY[@object.priority] %> > > -Ezra > > On Oct 1, 2005, at 2:32 AM, Kris Leech wrote: > >> Thanks Howard, but my status field is likely to have several >> status''s, I should have made that clearer. I was thinking a combo >> box would work well. >> >> Therefore if I use a int field type to store the status how do I >> convert the number to a text string to display in a view (using a >> helper method?) and how do I display a combo box to allow a user to >> choose a status which can then be sent to the create or edit method >> as the appropriate number? >> >> Thanks, K. >> >> Howard Roberts wrote: >> >> >>> On Fri, 2005-09-30 at 19:16 +0100, Kris Leech wrote: >>> kris, >>> >>>> Should you use: >>>> >>>> 1.) A varchar and store the actual status eg. "open", "closed" >>>> 2.) Use an field type of int and store an number which can be >>>> display as a status using a helper method >>>> 3.) Use a field type of int and use a table which maps an id to a >>>> status >>>> >>>> >>> 2.) Has the definite advantage in that in your view you can use the >>> checkbox helper, like so: >>> <%= check_box ''status'', ''closed'' %> >>> which will set your the closed attribute of your status model to 0 if >>> the box is not checked, or 1 if it is >>> >>> HTH, >>> Howard >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >>> >>> >> >> >> -- >> Interkonect Services UK Ltd. >> Boundary House Main Street >> Hoveringham >> Nottingham >> NG147 JR >> >> web: www.interkonect.com >> tel: 0115 9663696 >> fax: 0115 9663696 >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > -Ezra Zygmuntowicz > WebMaster > Yakima Herald-Republic Newspaper > ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org > 509-577-7732 > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >-- Interkonect Services UK Ltd. Boundary House Main Street Hoveringham Nottingham NG147 JR web: www.interkonect.com tel: 0115 9663696 fax: 0115 9663696
Julian ''Julik'' Tarkhanov
2005-Oct-02 18:21 UTC
Re: varchar or int field type for status field?
On 2-okt-2005, at 17:01, Kris Leech wrote:> Thanks Ezra, that works great :) > > I like this method but I have two problems the first is how to > construct the conditions for a find so that tasks with a certain > priority, say ''deleted'' are not shown. If I knew the number for the > priority I wanted to filter on then that would be fine, but what if > I change my order in the array at a later date. Really I need to > specify a string eg "deleted" and have it converted to the number > for use in a find.You can settle for a hash. class Foo < ActiveRecord::Base STATUS={:modified=>1, :blocked=>2} end @blocked_foos = Foo.find_all_by_status(Foo::STATUS[:blocked]) ...or check a recent thread on enumerations. -- Julian "Julik" Tarkhanov
Kris- In order to get the integers from the priority array you can use Array.index here''s what ri has to say about it: array.index(obj) -> int or nil ------------------------------------------------------------------------ Returns the index of the first object in self such that is == to obj. Returns nil if no match is found. a = [ "a", "b", "c" ] a.index("b") #=> 1 a.index("z") #=> nil So you could do this :> @tasks = Task.find(:all, :conditions => ["priority != ?", > TASK_STATUS.index(''deleted'')]) >> TASK_STATUS.index(''deleted'') will return the integer that refers > to the ''deleted'' item in the array.HTH -Ezra On Oct 2, 2005, at 8:01 AM, Kris Leech wrote:> Thanks Ezra, that works great :) > > I like this method but I have two problems the first is how to > construct the conditions for a find so that tasks with a certain > priority, say ''deleted'' are not shown. If I knew the number for the > priority I wanted to filter on then that would be fine, but what if > I change my order in the array at a later date. Really I need to > specify a string eg "deleted" and have it converted to the number > for use in a find. > > @tasks = Task.find(:all, :conditions => ["priority != ?", ???]) > > The second problem is when displaying the combo box how do I have > the correct option selected if a status already exists (eg. editing > a record). > > <select id="task_status" name="task[status]"> > <option value="0"><%= TASK_STATUS[0] %></option> > <option value="1"><%= TASK_STATUS[1] %></option> > <option value="2"><%= TASK_STATUS[2] %></option> > <option value="3"><%= TASK_STATUS[3] %></option> > <option value="4"><%= TASK_STATUS[4] %></option> </select> > > Would I insert something like: > > <option value="0" <%= if (@task.priority = 0) then puts " > ''selected''" end %>><%= TASK_STATUS[0] %></option> > > > > Ezra Zygmuntowicz wrote: > > >> Hey Kris- >> You could do something like this: First lets say you need a >> priority field to be a combo box on display but an int filed in >> the db: >> Define this array in the bottom of your environment.rb: >> >> PRIORITY = [''High'', ''Medium'', ''Low'', ''Very Low''] >> >> Now you have your field in the db as an int (1) and you store the >> numbers 0-3 in it. Then you could build a combo box like so. >> >> <select name="priority"> >> <option value="0">PRIORITY[0]</option> >> <option value="1">PRIORITY[1]</option> >> <option value="2">PRIORITY[2]</option> >> <option value="3">PRIORITY[3]</option> >> </select> >> >> Hope that helps some. Then whenever you want to display the >> priority that was already set, say in your list view just do: >> >> Priority: <%= PRIORITY[@object.priority] %> >> >> -Ezra >> >> On Oct 1, 2005, at 2:32 AM, Kris Leech wrote: >> >> >>> Thanks Howard, but my status field is likely to have several >>> status''s, I should have made that clearer. I was thinking a >>> combo box would work well. >>> >>> Therefore if I use a int field type to store the status how do I >>> convert the number to a text string to display in a view (using >>> a helper method?) and how do I display a combo box to allow a >>> user to choose a status which can then be sent to the create or >>> edit method as the appropriate number? >>> >>> Thanks, K. >>> >>> Howard Roberts wrote: >>> >>> >>> >>>> On Fri, 2005-09-30 at 19:16 +0100, Kris Leech wrote: >>>> kris, >>>> >>>> >>>>> Should you use: >>>>> >>>>> 1.) A varchar and store the actual status eg. "open", "closed" >>>>> 2.) Use an field type of int and store an number which can be >>>>> display as a status using a helper method >>>>> 3.) Use a field type of int and use a table which maps an id to >>>>> a status >>>>> >>>>> >>>>> >>>> 2.) Has the definite advantage in that in your view you can use >>>> the checkbox helper, like so: >>>> <%= check_box ''status'', ''closed'' %> >>>> which will set your the closed attribute of your status model to >>>> 0 if >>>> the box is not checked, or 1 if it is >>>> >>>> HTH, >>>> Howard >>>> >>>> _______________________________________________ >>>> Rails mailing list >>>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>> http://lists.rubyonrails.org/mailman/listinfo/rails >>>> >>>> >>>> >>>> >>>> >>> >>> >>> -- >>> Interkonect Services UK Ltd. >>> Boundary House Main Street >>> Hoveringham >>> Nottingham >>> NG147 JR >>> >>> web: www.interkonect.com >>> tel: 0115 9663696 >>> fax: 0115 9663696 >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >> >> -Ezra Zygmuntowicz >> WebMaster >> Yakima Herald-Republic Newspaper >> ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org >> 509-577-7732 >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> > > > -- > Interkonect Services UK Ltd. > Boundary House Main Street > Hoveringham > Nottingham > NG147 JR > > web: www.interkonect.com > tel: 0115 9663696 > fax: 0115 9663696 > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org 509-577-7732
Thanks both for those answers, I guess it boils down to knowing ruby better :) Or knowing rails common practices... Is there anywhere that documents ''best practice'' for these kind of questions? I know of the rails api and wiki. Thanks again, K.