1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add CHANGELOG entry for type casting enum values by the subtype

Related to #35336.

The notable thing about #41516 is that unknown labels will no longer
match 0 on MySQL.

Matching unknown labels to 0 was not by design, but rather almost like a
bug, people should not rely on that behavior.

Closes #41595.
This commit is contained in:
Ryuta Kamizono 2021-03-03 17:45:39 +09:00
parent 41970d254a
commit b68f095478
2 changed files with 32 additions and 0 deletions

View file

@ -1,3 +1,33 @@
* Type cast enum values by the original attribute type.
The notable thing about this change is that unknown labels will no longer match 0 on MySQL.
```ruby
class Book < ActiveRecord::Base
enum :status, { proposed: 0, written: 1, published: 2 }
end
```
Before:
```ruby
# SELECT `books`.* FROM `books` WHERE `books`.`status` = 'prohibited' LIMIT 1
Book.find_by(status: :prohibited)
# => #<Book id: 1, status: "proposed", ...> (for mysql2 adapter)
# => ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR: invalid input syntax for type integer: "prohibited" (for postgresql adapter)
# => nil (for sqlite3 adapter)
```
After:
```ruby
# SELECT `books`.* FROM `books` WHERE `books`.`status` IS NULL LIMIT 1
Book.find_by(status: :prohibited)
# => nil (for all adapters)
```
*Ryuta Kamizono*
* Fixtures for `has_many :through` associations now load timestamps on join tables * Fixtures for `has_many :through` associations now load timestamps on join tables
Given this fixture: Given this fixture:

View file

@ -80,6 +80,7 @@ class EnumTest < ActiveRecord::TestCase
assert_not_equal @book, Book.where.not(status: :published).first assert_not_equal @book, Book.where.not(status: :published).first
assert_equal @book, Book.where.not(status: :written).first assert_equal @book, Book.where.not(status: :written).first
assert_equal books(:ddd), Book.where(last_read: :forgotten).first assert_equal books(:ddd), Book.where(last_read: :forgotten).first
assert_nil Book.where(status: :prohibited).first
end end
test "find via where with strings" do test "find via where with strings" do
@ -90,6 +91,7 @@ class EnumTest < ActiveRecord::TestCase
assert_not_equal @book, Book.where.not(status: "published").first assert_not_equal @book, Book.where.not(status: "published").first
assert_equal @book, Book.where.not(status: "written").first assert_equal @book, Book.where.not(status: "written").first
assert_equal books(:ddd), Book.where(last_read: "forgotten").first assert_equal books(:ddd), Book.where(last_read: "forgotten").first
assert_nil Book.where(status: "prohibited").first
end end
test "find via where should be type casted" do test "find via where should be type casted" do