Hi,
I noticed that when you set a numeric column''s attribute to an empty
string, the attribute before_type_cast doesn''t reflect that value but
returns nil instead:
> item = Item.new
> e.cents = " "
> e.cents_before_type_cast
nil*
*
This brings the following remarks:
- if someone wants to provide a different validation for " " and nil
(like I had to do twice recently), they can''t rely on before_type_cast
for
that. They''ll have to work-around it by using a custom
accessor<https://github.com/thbar/activerecord-experiments/blob/master/lib/decimal_custom_accessor_price_product.rb>for
instance
- there''s what seems to be a hint of this data
massage<https://github.com/rails/rails/blob/4bbd05c90e5a62c0316307b3ca7c2262ed98b567/activemodel/lib/active_model/validations/numericality.rb#L95>
of
the before_type_cast in the documentation
- the behavior from the user point of view differs from ActiveModel
validates_numericality_of which will reject a blank
value<https://github.com/rails/rails/blob/0fe311a7fcf1457b9a72f99f887f756a28a53db4/activemodel/test/cases/validations/numericality_validation_test.rb#L32>,
if I''m not mistaken
- more generally, one can then wonder if there are other data massaging of
before_type_cast attributes happening on other types or contexts (I
didn''t
find any so far though)
Can someone with more knowledge of the internals and the history of
ActiveRecord shed some light on this?
Is consistency here desirable and worth pursuing?
Alternatively, should this be documented alongside with before_type_cast in
the
documentation<https://github.com/rails/rails/blob/9cd1f697f1b4dded0f18e13c7ae12c7bd2dab1c0/activerecord/lib/active_record/base.rb#L150>
?
-- Thibaut
--
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 rubyonrails-core@googlegroups.com.
To unsubscribe from this group, send email to
rubyonrails-core+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en.