mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fixed a bug where NULLs are casted into the first enum value
This commit is contained in:
parent
53f3803b52
commit
e076d7290d
2 changed files with 9 additions and 3 deletions
|
@ -106,6 +106,7 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def type_cast_from_database(value)
|
||||
return if value.nil?
|
||||
mapping.key(value.to_i)
|
||||
end
|
||||
|
||||
|
|
|
@ -168,19 +168,24 @@ class EnumTest < ActiveRecord::TestCase
|
|||
assert_equal "'unknown' is not a valid status", e.message
|
||||
end
|
||||
|
||||
test "NULL values from database should be casted to nil" do
|
||||
Book.where(id: @book.id).update_all("status = NULL")
|
||||
assert_nil @book.reload.status
|
||||
end
|
||||
|
||||
test "assign nil value" do
|
||||
@book.status = nil
|
||||
assert @book.status.nil?
|
||||
assert_nil @book.status
|
||||
end
|
||||
|
||||
test "assign empty string value" do
|
||||
@book.status = ''
|
||||
assert @book.status.nil?
|
||||
assert_nil @book.status
|
||||
end
|
||||
|
||||
test "assign long empty string value" do
|
||||
@book.status = ' '
|
||||
assert @book.status.nil?
|
||||
assert_nil @book.status
|
||||
end
|
||||
|
||||
test "constant to access the mapping" do
|
||||
|
|
Loading…
Reference in a new issue