From 1519e976b224871c7f7dd476351930d5d0d7faf6 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Mon, 17 Jul 2017 11:14:11 -0400 Subject: Allow multiparameter assigned attributes to be used with `text_field` Between 4.2 and 5.0 the behavior of how multiparameter attributes interact with `_before_type_cast` changed. In 4.2 it returns the post-type-cast value. After 5.0, it returns the hash that gets sent to the type. This behavior is correct, but will cause an issue if you then tried to render that value in an input like `text_field` or `hidden_field`. In this case, we want those fields to use the post-type-cast form, instead of the `_before_type_cast` (the main reason it uses `_before_type_cast` at all is to avoid losing data when casting a non-numeric string to integer). I've opted to modify `came_from_user?` rather than introduce a new method for this as I want to avoid complicating that contract further, and technically the multiparameter hash didn't come from assignment, it was constructed internally by AR. Close #27888. --- activerecord/lib/active_record/attribute.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/attribute.rb b/activerecord/lib/active_record/attribute.rb index 78662433eb..f89261923b 100644 --- a/activerecord/lib/active_record/attribute.rb +++ b/activerecord/lib/active_record/attribute.rb @@ -174,7 +174,7 @@ module ActiveRecord end def came_from_user? - true + !type.value_constructed_by_mass_assignment?(value_before_type_cast) end end -- cgit v1.2.3