Hello, What is the best way to to do enumerations of values that you want to use to provide values for a field. There are many cases where you want the user to choose from a list of values, but you really don''t need a whole model to support. Yet, it should be flexible enough to able to be changed. A simple example would be a gender field in a person record. There needs to be somewhere to store the ''male'' and ''female'' values to be used in a select field, but we don''t want to hard code these values because later we might add ''unknown'' as an option, or change to ''boy'' and ''girl''. With only two values it seems pointless to have a whole model dedicated to this, yet it still needs to be somewhat dynamic as the values could change. What is the best practice for this type of data? Matt -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jun 11, 2012, at 4:45 PM, Matt Martini wrote:> Hello, > > What is the best way to to do enumerations of values that you want to use to provide values for a field. There are many cases where you want the user to choose from a list of values, but you really don''t need a whole model to support. Yet, it should be flexible enough to able to be changed. > > A simple example would be a gender field in a person record. There needs to be somewhere to store the ''male'' and ''female'' values to be used in a select field, > but we don''t want to hard code these values because later we might add ''unknown'' as an option, or change to ''boy'' and ''girl''. With only two values it seems pointless to have a whole model dedicated to this, yet it still needs to be somewhat dynamic as the values could change. > > What is the best practice for this type of data?I don''t know about best practice, but I often stick these in global vars, usually in the application.rb file or similar. Walter> > Matt > > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Don''t know what the best practice is, but the way I do it is in the model class User < ActiveRecord::Base UserGenders = {:male => 1, :female => 2} end Then from code I just do if user.gender_id == User::UserGenders[:male] On Mon, Jun 11, 2012 at 1:45 PM, Matt Martini <matt.martini-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> Hello, > > What is the best way to to do enumerations of values that you want to use > to provide values for a field. There are many cases where you want the > user to choose from a list of values, but you really don''t need a whole > model to support. Yet, it should be flexible enough to able to be changed. > > A simple example would be a gender field in a person record. There needs > to be somewhere to store the ''male'' and ''female'' values to be used in a > select field, > but we don''t want to hard code these values because later we might add > ''unknown'' as an option, or change to ''boy'' and ''girl''. With only two > values it seems pointless to have a whole model dedicated to this, yet it > still needs to be somewhat dynamic as the values could change. > > What is the best practice for this type of data? > > Matt > > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- ~ Jeremiah:9:23-24 Android 2D MMORPG: http://solrpg.com/, http://www.youtube.com/user/revoltingx -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I do it in the model, too, and also validate that nothing else can get in that column in the database: GENDERS = %w(Male Female) validates_inclusion_of :gender, :in => GENDERS On Mon, Jun 11, 2012 at 4:57 PM, Miguel Morales <therevoltingx-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Don''t know what the best practice is, but the way I do it is in the model > > class User < ActiveRecord::Base > UserGenders = {:male => 1, :female => 2} > end > > Then from code I just do if user.gender_id == User::UserGenders[:male] > > > On Mon, Jun 11, 2012 at 1:45 PM, Matt Martini <matt.martini-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> >> Hello, >> >> What is the best way to to do enumerations of values that you want to use >> to provide values for a field. There are many cases where you want the user >> to choose from a list of values, but you really don''t need a whole model to >> support. Yet, it should be flexible enough to able to be changed. >> >> A simple example would be a gender field in a person record. There needs >> to be somewhere to store the ''male'' and ''female'' values to be used in a >> select field, >> but we don''t want to hard code these values because later we might add >> ''unknown'' as an option, or change to ''boy'' and ''girl''. With only two values >> it seems pointless to have a whole model dedicated to this, yet it still >> needs to be somewhat dynamic as the values could change. >> >> What is the best practice for this type of data? >> >> Matt >> >> >> -- >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> > > > > -- > ~ Jeremiah:9:23-24 > Android 2D MMORPG: > http://solrpg.com/, http://www.youtube.com/user/revoltingx > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Guys, Thanks for the feedback. Would these approaches still be the way to go if you had a large-ish set of data, say State names of Country Names? Matt On Jun 11, 2012, at 5:37 PM, Paul wrote:> I do it in the model, too, and also validate that nothing else can get > in that column in the database: > > GENDERS = %w(Male Female) > validates_inclusion_of :gender, :in => GENDERS > > > On Mon, Jun 11, 2012 at 4:57 PM, Miguel Morales <therevoltingx-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Don''t know what the best practice is, but the way I do it is in the model >> >> class User < ActiveRecord::Base >> UserGenders = {:male => 1, :female => 2} >> end >> >> Then from code I just do if user.gender_id == User::UserGenders[:male] >> >> >> On Mon, Jun 11, 2012 at 1:45 PM, Matt Martini <matt.martini-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >> wrote: >>> >>> Hello, >>> >>> What is the best way to to do enumerations of values that you want to use >>> to provide values for a field. There are many cases where you want the user >>> to choose from a list of values, but you really don''t need a whole model to >>> support. Yet, it should be flexible enough to able to be changed. >>> >>> A simple example would be a gender field in a person record. There needs >>> to be somewhere to store the ''male'' and ''female'' values to be used in a >>> select field, >>> but we don''t want to hard code these values because later we might add >>> ''unknown'' as an option, or change to ''boy'' and ''girl''. With only two values >>> it seems pointless to have a whole model dedicated to this, yet it still >>> needs to be somewhat dynamic as the values could change. >>> >>> What is the best practice for this type of data? >>> >>> Matt >>> >>> >>> -- >>> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> To unsubscribe from this group, send email to >>> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> For more options, visit this group at >>> http://groups.google.com/group/rubyonrails-talk?hl=en. >>> >> >> >> >> -- >> ~ Jeremiah:9:23-24 >> Android 2D MMORPG: >> http://solrpg.com/, http://www.youtube.com/user/revoltingx >> >> -- >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jun 11, 2012, at 5:57 PM, Matt Martini wrote:> Guys, > > Thanks for the feedback. > > Would these approaches still be the way to go if you had a large-ish set of data, > say State names of Country Names? >That would depend on whether you needed to have related data, like this country has these states or anything like that. At that point you might want to move this into YAML or similar. There''s a Railscast about this IIRC. Walter> Matt > > > On Jun 11, 2012, at 5:37 PM, Paul wrote: > >> I do it in the model, too, and also validate that nothing else can get >> in that column in the database: >> >> GENDERS = %w(Male Female) >> validates_inclusion_of :gender, :in => GENDERS >> >> >> On Mon, Jun 11, 2012 at 4:57 PM, Miguel Morales <therevoltingx-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> Don''t know what the best practice is, but the way I do it is in the model >>> >>> class User < ActiveRecord::Base >>> UserGenders = {:male => 1, :female => 2} >>> end >>> >>> Then from code I just do if user.gender_id == User::UserGenders[:male] >>> >>> >>> On Mon, Jun 11, 2012 at 1:45 PM, Matt Martini <matt.martini-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>> wrote: >>>> >>>> Hello, >>>> >>>> What is the best way to to do enumerations of values that you want to use >>>> to provide values for a field. There are many cases where you want the user >>>> to choose from a list of values, but you really don''t need a whole model to >>>> support. Yet, it should be flexible enough to able to be changed. >>>> >>>> A simple example would be a gender field in a person record. There needs >>>> to be somewhere to store the ''male'' and ''female'' values to be used in a >>>> select field, >>>> but we don''t want to hard code these values because later we might add >>>> ''unknown'' as an option, or change to ''boy'' and ''girl''. With only two values >>>> it seems pointless to have a whole model dedicated to this, yet it still >>>> needs to be somewhat dynamic as the values could change. >>>> >>>> What is the best practice for this type of data? >>>> >>>> Matt >>>> >>>> >>>> -- >>>> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>>> To unsubscribe from this group, send email to >>>> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>>> For more options, visit this group at >>>> http://groups.google.com/group/rubyonrails-talk?hl=en. >>>> >>> >>> >>> >>> -- >>> ~ Jeremiah:9:23-24 >>> Android 2D MMORPG: >>> http://solrpg.com/, http://www.youtube.com/user/revoltingx >>> >>> -- >>> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> To unsubscribe from this group, send email to >>> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> For more options, visit this group at >>> http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> -- >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >> > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.