powertoaster
2010-Sep-02 20:21 UTC
ActiveRecord::ConnectionAdapters::Column value_to_boolean(value) does not return a boolean value.
value_to_boolean(value), does not always return a boolean value, only when true. 150: def value_to_boolean(value) 151: if value.is_a?(String) && value.blank? 152: nil 153: else 154: TRUE_VALUES.include?(value) 155: end 156: end If value is contained in TRUE_VALUES then it returns true(TrueClass). But if value is anything else it returns nil(NilClass) instead of false(FalseClass) This does not seem consistent or expected to me since false <> nil. Since we have TRUE_VALUES and FALSE_VALUES, I think it should be something like this. 150 def value_to_boolean(value) 151 if TRUE_VALUES.include?(value) 152 true 153 elsif FALSE_VALUES.include?(value) 154 false 155 else 156 nil 157 end 158 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-/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.
Frederick Cheung
2010-Sep-03 13:59 UTC
Re: ActiveRecord::ConnectionAdapters::Column value_to_boolean(value) does not return a boolean value.
On Sep 2, 9:21 pm, powertoaster <powertoas...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > If value is contained in TRUE_VALUES then it returns true(TrueClass). > But if value is anything else it returns nil(NilClass) instead of > false(FalseClass) > > This does not seem consistent or expected to me since false <> nil. > > Since we have TRUE_VALUES and FALSE_VALUES, I think it should be > something like this. >The code as it is ensures that if the column contains NULL then you get nil out. 3 way true/false/unset logic can be a little odd but I''m not sure whether it should just be completely supressed. It might also be misleading in that foo.flag == false leads you to believe that Foo.find_all_by_flag(false) would return that row, which it wouldn''t if the flag is NULL. If you don''t like NULLs in boolean columns, then make it a non null column (which i usually do) Fred -- 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.