I ran into some very strange behavior just a second ago. I have an ENUM field which I named "dummy" CREATE TABLE `foobars` ( `id` int(6) unsigned NOT NULL auto_increment, `dummy` enum(''foo'',''bar'',''baz'',''quux'') NOT NULL default ''foo'', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; CREATE TABLE `foobars` ( `id` int(6) unsigned NOT NULL auto_increment, `dummy` enum(''acquaintance'',''foo'',''bar'',''baz'',''quux'') NOT NULL default ''foo'', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; When you display the associated pages, the first foobar gives what you would expect. However, on the second table foobar.dummy always returns 0, regardless of the actual values of the rows in the database. If you change the "acquaintance" value in any way, make it longer, shorter, change a letter to something else, doesn''t matter, then it goes back to working. This seems..... wrong.... somehow. -- Bob Aman
> When you display the associated pages, the first foobar gives what you > would expect. > However, on the second table foobar.dummy always returns 0, regardless > of the actual values of the rows in the database. If you change the > "acquaintance" value in any way, make it longer, shorter, change a > letter to something else, doesn''t matter, then it goes back to > working. > > This seems..... wrong.... somehow.I''m finding several other circumstances where similar behavior appears... is there something wrong with how Rails handles enums? -- Bob Aman
> This seems..... wrong.... somehow.Apparently, rails doesn''t really support the MySQL enum type and I should use a varchar instead. Probably should have googled for more information. However, I''m still confused as to why I would get this eratic behavior, even if enums weren''t supported. Wouldn''t it make more sense to throw an exception if dealing with an enum? Coming back with 0 in inexplicable circumstances but working fine in others strikes me as a Bad Thing(tm). But then again, I''m a noob, what do I know? -- Bob Aman
AR doesn''t support enum data type. It''s recommended that you use normal varchar and use rails validations (esp. validates_inclusion_of, http://rails.rubyonrails.com/classes/ActiveRecord/Validations/ ClassMethods.html#M000350) to check that the values are within allowed values. //jarkko On 18.2.2005, at 19:24, Bob Aman wrote:>> This seems..... wrong.... somehow. > > Apparently, rails doesn''t really support the MySQL enum type and I > should use a varchar instead. Probably should have googled for more > information. However, I''m still confused as to why I would get this > eratic behavior, even if enums weren''t supported. Wouldn''t it make > more sense to throw an exception if dealing with an enum? Coming back > with 0 in inexplicable circumstances but working fine in others > strikes me as a Bad Thing(tm). > > But then again, I''m a noob, what do I know? > > -- > Bob Aman > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Friday 18 February 2005 18:32, Jarkko Laine wrote:> http://rails.rubyonrails.com/classes/ActiveRecord/Validations/http://ar.rubyonrails.com/classes/ActiveRecord/Validations/ :-) -- Dominic
> > http://rails.rubyonrails.com/classes/ActiveRecord/Validations/ > > http://ar.rubyonrails.com/classes/ActiveRecord/Validations/I don''t know about the rest of you guys, but I get 403 errors on both of those links. -- Bob Aman
On Friday 18 February 2005 20:47, Bob Aman wrote:> > > http://rails.rubyonrails.com/classes/ActiveRecord/Validations/ > > > > http://ar.rubyonrails.com/classes/ActiveRecord/Validations/ > > I don''t know about the rest of you guys, but I get 403 errors on both > of those links.I did on rails, then ar. worked. I suppose you just have to find it yourself :-) Browse from http://ar.rubyonrails.com/ -- Dominic
> I did on rails, then ar. worked. I suppose you just have to find it > yourself :-) > > Browse from http://ar.rubyonrails.com/http://ar.rubyonrails.com/classes/ActiveRecord/Validations.html In any case, lacking the underlying ruby knowledge, I''m not sure exactly how to search through the array of valid string values and check if the input is among the entries. Is there a convenient .contains? method? -- Bob Aman
On Fri, 18 Feb 2005, Bob Aman wrote:>> I did on rails, then ar. worked. I suppose you just have to find it >> yourself :-) >> >> Browse from http://ar.rubyonrails.com/ > > http://ar.rubyonrails.com/classes/ActiveRecord/Validations.html > > In any case, lacking the underlying ruby knowledge, I''m not sure > exactly how to search through the array of valid string values and > check if the input is among the entries. Is there a convenient > .contains? method?valid = %w( one two three ) chosen = @params[''chosen''] present = valid.include? chosen # => true or false better (case insensitive) pattern = %r/#{ choice }/i present = not valid.select{|choice| choice =~ chosen}.empty? hth. -a -- ==============================================================================| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki ===============================================================================
On 18.2.2005, at 23:06, Ara.T.Howard wrote:> > valid = %w( one two three ) > > chosen = @params[''chosen''] > > present = valid.include? chosen # => true or false > > better (case insensitive) > > pattern = %r/#{ choice }/i > > present = not valid.select{|choice| choice =~ chosen}.empty?Ara, You don''t have to go in such details. You can just use the in-built validation functions Rails offers (http://rails.rubyonrails.com/classes/ActiveRecord/Validations/ ClassMethods.html) In this case: validates_inclusion_of :chosen, :in => valid Put that (and the definition of valid) in your model class and you''ll be fine. //jarkko> > hth. > > -a > -- > ======================================================================= > =======> | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov > | PHONE :: 303.497.6469 > | When you do something, you should burn yourself completely, like a > good > | bonfire, leaving no trace of yourself. --Shunryu Suzuki > ======================================================================= > =======> _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Fri, 18 Feb 2005, Jarkko Laine wrote:> On 18.2.2005, at 23:06, Ara.T.Howard wrote: >> >> valid = %w( one two three ) >> >> chosen = @params[''chosen''] >> >> present = valid.include? chosen # => true or false >> >> better (case insensitive) >> >> pattern = %r/#{ choice }/i >> >> present = not valid.select{|choice| choice =~ chosen}.empty? > > Ara, > > You don''t have to go in such details. You can just use the in-built > validation functions Rails offers > (http://rails.rubyonrails.com/classes/ActiveRecord/Validations/ > ClassMethods.html) > > In this case: > validates_inclusion_of :chosen, :in => valid > > Put that (and the definition of valid) in your model class and you''ll be > fine. > > //jarkkoright you are. OP seemed to be specifically asking about a method on array objects though. sorry for noise. -a -- ==============================================================================| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki ===============================================================================
> > In this case: > > validates_inclusion_of :chosen, :in => valid > > > > Put that (and the definition of valid) in your model class and you''ll be > > fine. > > > > //jarkko > > right you are. OP seemed to be specifically asking about a method on array > objects though. sorry for noise.Yeah, the built in functions where exactly what I was looking for. -- Bob Aman
> > In this case: > > validates_inclusion_of :chosen, :in => valid > > > > Put that (and the definition of valid) in your model class and you''ll be > > fine. > > > > //jarkko > > right you are. OP seemed to be specifically asking about a method on array > objects though. sorry for noise.Yeah, the built in functions were exactly what I was looking for. -- Bob Aman