提交 96e504ec 编写于 作者: S Sean Griffin

Errors raised in `type_cast_for_database` no longer raise on assignment

Fixes #18580.
上级 b9d668f8
* 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*
......
......@@ -165,7 +165,7 @@ def original_raw_attributes
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
......
......@@ -53,7 +53,7 @@ def cast_value(value)
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
......
......@@ -117,6 +117,23 @@ def test_binary_encoding
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册