diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index b87f24bb6d..3e173b930c 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -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) + # => # (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 Given this fixture: diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb index c50833b62f..140712a657 100644 --- a/activerecord/test/cases/enum_test.rb +++ b/activerecord/test/cases/enum_test.rb @@ -80,6 +80,7 @@ class EnumTest < ActiveRecord::TestCase assert_not_equal @book, Book.where.not(status: :published).first assert_equal @book, Book.where.not(status: :written).first assert_equal books(:ddd), Book.where(last_read: :forgotten).first + assert_nil Book.where(status: :prohibited).first end 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_equal @book, Book.where.not(status: "written").first assert_equal books(:ddd), Book.where(last_read: "forgotten").first + assert_nil Book.where(status: "prohibited").first end test "find via where should be type casted" do