Use non-exist enum string to get unrelated record in My SQL
This behaviour is in rails/activerecord/lib/active_record/enum.rb #serialize(value) line no 143 if value is not present in mapping we are sending the value back , which in mysql returns unrelated record. I have changed to return nil is value is not present in mapping Implemented code review changes Improved test case coverage [ci skip] - cosmetic changes for better readibility of change log Signed-off-by: ak <atulkanswal@gmail.com>
This commit is contained in:
parent
08dfa9212d
commit
93b8c24f18
@ -1,3 +1,16 @@
|
||||
* Raise error when non-existent enum used in query
|
||||
|
||||
This change will raise an error when a non-existent enum is passed to a query. Previously
|
||||
with MySQL this would return an unrelated record. Fixes #38687.
|
||||
```ruby
|
||||
class User < ActiveRecord::Base
|
||||
enum status: { active: 0, non_active: 1 }
|
||||
end
|
||||
User.where(status: :non_existing_status)
|
||||
=> ArgumentError ('non_existing_status' is not a valid status)
|
||||
```
|
||||
*Atul Kanswal *
|
||||
|
||||
* Dump the schema or structure of a database when calling db:migrate:name
|
||||
|
||||
In previous versions of Rails, `rails db:migrate` would dump the schema of the database. In Rails 6, that holds true (`rails db:migrate` dumps all databases' schemas), but `rails db:migrate:name` does not share that behavior.
|
||||
|
@ -140,6 +140,7 @@ def deserialize(value)
|
||||
end
|
||||
|
||||
def serialize(value)
|
||||
assert_valid_value(value)
|
||||
mapping.fetch(value, value)
|
||||
end
|
||||
|
||||
|
@ -59,6 +59,7 @@ class EnumTest < ActiveRecord::TestCase
|
||||
assert_not_equal @book, Book.where(status: [written]).first
|
||||
assert_not_equal @book, Book.where("status <> ?", published).first
|
||||
assert_equal @book, Book.where("status <> ?", written).first
|
||||
assert_empty Book.where(status: nil)
|
||||
end
|
||||
|
||||
test "find via where with symbols" do
|
||||
@ -69,6 +70,8 @@ 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(read_status: :forgotten).first
|
||||
exception = assert_raises(ArgumentError) { Book.where(status: :not_defined).first }
|
||||
assert_match(/'not_defined' is not a valid status/, exception.message)
|
||||
end
|
||||
|
||||
test "find via where with strings" do
|
||||
@ -79,6 +82,8 @@ 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(read_status: "forgotten").first
|
||||
exception = assert_raises(ArgumentError) { Book.where(status: "not_defined").first }
|
||||
assert_match(/'not_defined' is not a valid status/, exception.message)
|
||||
end
|
||||
|
||||
test "build from scope" do
|
||||
|
Loading…
Reference in New Issue
Block a user