diff options
author | Sean Griffin <sean@thoughtbot.com> | 2015-01-23 12:47:41 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2015-01-23 12:50:01 -0700 |
commit | 96e504ec8af149962312c13dd418e13e4c74ce86 (patch) | |
tree | edc8ce3f4de566ec5c1e90472957d5b381f10547 | |
parent | b9d668f8cb466ab70e107e8ed6e1df2d28c25f31 (diff) | |
download | rails-96e504ec8af149962312c13dd418e13e4c74ce86.tar.gz rails-96e504ec8af149962312c13dd418e13e4c74ce86.tar.bz2 rails-96e504ec8af149962312c13dd418e13e4c74ce86.zip |
Errors raised in `type_cast_for_database` no longer raise on assignment
Fixes #18580.
-rw-r--r-- | activerecord/CHANGELOG.md | 11 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/dirty.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/types_test.rb | 17 |
4 files changed, 29 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 63cd715920..2054c9573e 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,6 +1,15 @@ +* Values which would error while being sent to the database (such as an + ASCII-8BIT string with invalid UTF-8 bytes on Sqlite3), no longer error on + assignment. They will still error when sent to the database, but you are + given the ability to re-assign it to a valid value. + + Fixes #18580. + + *Sean Griffin* + * Don't remove join dependencies in `Relation#exists?` - Fixes #18632 + Fixes #18632. *Sean Griffin* diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb index bce9c5e1e3..06d87ee01e 100644 --- a/activerecord/lib/active_record/attribute_methods/dirty.rb +++ b/activerecord/lib/active_record/attribute_methods/dirty.rb @@ -165,7 +165,7 @@ module ActiveRecord end def store_original_raw_attribute(attr_name) - original_raw_attributes[attr_name] = @attributes[attr_name].value_for_database + original_raw_attributes[attr_name] = @attributes[attr_name].value_for_database rescue nil end def store_original_raw_attributes diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index 03dfd29a0a..52dce6291a 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -53,7 +53,7 @@ module ActiveRecord class SQLite3String < Type::String # :nodoc: def type_cast_for_database(value) if value.is_a?(::String) && value.encoding == Encoding::ASCII_8BIT - value.encode(Encoding::UTF_8) + value.encode(Encoding::UTF_8, undef: :replace) else super end diff --git a/activerecord/test/cases/types_test.rb b/activerecord/test/cases/types_test.rb index 73e92addfe..d35d34ff2d 100644 --- a/activerecord/test/cases/types_test.rb +++ b/activerecord/test/cases/types_test.rb @@ -117,6 +117,23 @@ module ActiveRecord assert_equal Encoding::ASCII_8BIT, type_cast.encoding end end + + def test_attributes_which_are_invalid_for_database_can_still_be_reassigned + type_which_cannot_go_to_the_database = Type::Value.new + def type_which_cannot_go_to_the_database.type_cast_for_database(*) + raise + end + klass = Class.new(ActiveRecord::Base) do + self.table_name = 'posts' + attribute :foo, type_which_cannot_go_to_the_database + end + model = klass.new + + model.foo = "foo" + model.foo = "bar" + + assert_equal "bar", model.foo + end end end end |