Hi! I have a model which has a field that can have three values only. I store the value in the database as an integer, 0, 1 or 2, but when I display it I want a more appropriate text. And, I want it to be translatable using i18n. Let''s say that it corresponds to how difficult something is. So, 0 represents "Easy", 1 represents "Normal" and 2 represents "Hard". What I did was first to define it as a hash like this: DIFFICULTIES = { "Easy" => 0, "Normal" => 1, "Hard" => 2 } Then I can easily pass this to, for instance, simple_form and it will generate a nice dropdown with the correct values. But let''s say that I have the value and want to display the text. Then I''d have to iterate over the hash to find which key corresponds to the right value. Right? Not that big of an issue when there are three values as in this example, but there could be more. How do you normally handle these cases? Is there any "best practice" to handle this in an efficient manner? Cheers, Linus -- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/1fjJK_EjQ50J. For more options, visit https://groups.google.com/groups/opt_out.
On Sat, Jan 26, 2013 at 8:58 PM, Linus Pettersson < linus.pettersson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi! > > I have a model which has a field that can have three values only. I store > the value in the database as an integer, 0, 1 or 2, but when I display it I > want a more appropriate text. And, I want it to be translatable using i18n. > > Let''s say that it corresponds to how difficult something is. So, 0 > represents "Easy", 1 represents "Normal" and 2 represents "Hard". > What I did was first to define it as a hash like this: > DIFFICULTIES = { "Easy" => 0, "Normal" => 1, "Hard" => 2 } > Then I can easily pass this to, for instance, simple_form and it will > generate a nice dropdown with the correct values. > > But let''s say that I have the value and want to display the text. Then I''d > have to iterate over the hash to find which key corresponds to the right > value. Right? Not that big of an issue when there are three values as in > this example, but there could be more. >Use http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-key to get the key you want given a value. I''m thinking that you will only do this if you have a small number of constants that you want to define. If it is a long list, then use the database for that.> > How do you normally handle these cases? Is there any "best practice" to > handle this in an efficient manner? > > Cheers, > Linus > > -- > 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 > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/1fjJK_EjQ50J. > For more options, visit https://groups.google.com/groups/opt_out. > > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- 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 https://groups.google.com/groups/opt_out.
On 01/26, Linus Pettersson wrote:> Hi! > > I have a model which has a field that can have three values only. I store > the value in the database as an integer, 0, 1 or 2, but when I display it I > want a more appropriate text. And, I want it to be translatable using i18n. > > Let''s say that it corresponds to how difficult something is. So, 0 > represents "Easy", 1 represents "Normal" and 2 represents "Hard". > What I did was first to define it as a hash like this: > DIFFICULTIES = { "Easy" => 0, "Normal" => 1, "Hard" => 2 } > Then I can easily pass this to, for instance, simple_form and it will > generate a nice dropdown with the correct values. > > But let''s say that I have the value and want to display the text. Then I''d > have to iterate over the hash to find which key corresponds to the right > value. Right? Not that big of an issue when there are three values as in > this example, but there could be more.I don''t know of any best practice for this, but searching in a hash takes, on average, O(1); so I don''t think there''s anything to worry about here. Of course, if the hash gets big you might end up with maintainability issues, but that''s another story.> > How do you normally handle these cases? Is there any "best practice" to > handle this in an efficient manner? > > Cheers, > Linus > > -- > 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 > To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/1fjJK_EjQ50J. > For more options, visit https://groups.google.com/groups/opt_out. > >-- 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 https://groups.google.com/groups/opt_out.
On Sat, Jan 26, 2013 at 6:58 AM, Linus Pettersson <linus.pettersson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a model which has a field that can have three values only. I store > the value in the database as an integer, 0, 1 or 2, but when I display it I > want a more appropriate text. And, I want it to be translatable using i18n. > > Let''s say that it corresponds to how difficult something is. So, 0 > represents "Easy", 1 represents "Normal" and 2 represents "Hard". > What I did was first to define it as a hash like this: > DIFFICULTIES = { "Easy" => 0, "Normal" => 1, "Hard" => 2 } > Then I can easily pass this to, for instance, simple_form and it will > generate a nice dropdown with the correct values. > > But let''s say that I have the value and want to display the text. Then I''d > have to iterate over the hash to find which key corresponds to the right > value. Right? Not that big of an issue when there are three values as in > this example, but there could be more. > > How do you normally handle these cases? Is there any "best practice" to > handle this in an efficient manner?Not quite sure if I understand this entirely, but you''re looking to be able to output the associated text in some place *other* than simple_form? (i.e., when you''re displaying the record?) Seems like it should be straight-forward to map them to values in your locale file. no? en: difficulties: 0: Easy 1: Normal 2: Hard then just invoke: t ''difficulties.#{difficulty}'' where you want to display it? And maybe use this to build the Hash for simple_form to begin with? -- 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 https://groups.google.com/groups/opt_out.
Hey, You can have constants defined in a file called constants.rb in your initializers folder. DIFFICULTIES = { "Easy" => 0, "Normal" => 1, "Hard" => 2 , 0=>"Easy",1=>"Normal", 2=>"Hard"} This will help you to get your values both ways easily. On Saturday, 26 January 2013 18:28:36 UTC+5:30, Linus Pettersson wrote:> > Hi! > > I have a model which has a field that can have three values only. I store > the value in the database as an integer, 0, 1 or 2, but when I display it I > want a more appropriate text. And, I want it to be translatable using i18n. > > Let''s say that it corresponds to how difficult something is. So, 0 > represents "Easy", 1 represents "Normal" and 2 represents "Hard". > What I did was first to define it as a hash like this: > DIFFICULTIES = { "Easy" => 0, "Normal" => 1, "Hard" => 2 } > Then I can easily pass this to, for instance, simple_form and it will > generate a nice dropdown with the correct values. > > But let''s say that I have the value and want to display the text. Then I''d > have to iterate over the hash to find which key corresponds to the right > value. Right? Not that big of an issue when there are three values as in > this example, but there could be more. > > How do you normally handle these cases? Is there any "best practice" to > handle this in an efficient manner? > > Cheers, > Linus >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/3VIOZjNKUHYJ. For more options, visit https://groups.google.com/groups/opt_out.