Jarl, I'm wondering in what context you end up with user input that outputs a boolean?
Is it from a web form, or XML, or JSON or console app? My feeling is that you're only going to end up with a true/false if you are calling from a method inside your application, eg. something.make_order(params, :balloon => true) in which case "validation" may be the wrong use case. If you're trying to guard against your own input then perhaps what you want to invent something like guard_field :balloon, :values => [true, false] where saying order.balloon = "purple" would instead raise an ArgumentError On May 31, 5:08 pm, Jarl Friis <[email protected]> wrote: > Hi Michael. > > Thanks for replying. > > Michael Koziarski <[email protected]> writes: > >> In general I find it confusing that my validation takes place after > >> the column_specific type cast. I would rather have a validation > >> message when my boolean field is set to "some arbitraty text" instead > >> of letting it be swallowed by some fall_back value in > >> ActiveRecord::ConnectionAdapters::Column::value_to_boolean. > > >> In general I find it confusing and not clearly documented that > >> validations occur after the type_cast mechanism (is that true for > >> all types? also times/dates?) > > > Yeah, it happens for all types otherwise you couldn't do something like > > this: > > > validates_inclusion_of :price, :in=>0..500 > > I see. However it is unclear which validates_* macros that validates the raw > value and which that validates the type_casted value. > > > Form submission values are all strings and "30" wouldn't pass that > > validation if it were done before typecasting. Similarly your > > validation wouldn't work with check_box_tag. > > Well, this is exactly my problem, the current implementation does not > work with the suggested validation for booleans: > > The current suggested way to validate booleans is > validates_inclusion_of :balloon, :in => [true, false] > > But that does NOT work, providing "something else" as parameter to > :balloon passes the validation, becauase it is converted to false > before the validates_inclusion_of > > So for all boolean fields I must write sometning like this: > validates_each(:balloon) do |record,attr_name,value| > raw_value = record.send("#{attr_name}_before_type_cast") > unless > ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.union(ActiveRecord::ConnectionAdapters::Column::FALSE_VALUES).member? > raw_value > record.errors.add(attr_name, :inclusion, :default => "is not a boolean > value", :value => raw_value) > end > end > > I am quite surprised that I am the only/first one having this problem, > boolean values can't be that rare. I didn't do anything special, just > introduced a boolean field, and added some tests like (using shoulda): > > should_allow_values_for :balloon, true, false > should_not_allow_values_for :balloon, nil, '', 'any ohter value' > > And then the problem showed up. > > Jarl -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
