Merge pull request #51758 from taketo1113/fix-changed-cidr2

PostgreSQL Cidr#change? raise an error of NoMethodError: undefined method `prefix' for nil, when creating a record with an empty value of inet/cidr column.
This commit is contained in:
Carlos Antonio da Silva 2024-05-14 09:59:51 -03:00 committed by GitHub
commit 0be846c893
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 1 deletions

View File

@ -31,7 +31,7 @@ module ActiveRecord
# TODO: Remove when IPAddr#== compares IPAddr#prefix. See
# https://github.com/ruby/ipaddr/issues/21
def changed?(old_value, new_value, _new_value_before_type_cast)
super || old_value.prefix != new_value.prefix
super || !old_value.nil? && old_value.prefix != new_value.prefix
end
def cast_value(value)

View File

@ -21,6 +21,15 @@ module ActiveRecord
assert_equal "foo", type.serialize("foo")
end
test "changed? with nil values" do
type = OID::Cidr.new
assert_not type.changed?(nil, nil, "")
assert type.changed?("192.168.0.0/24", nil, "")
assert type.changed?(nil, "192.168.0.0/24", "")
assert type.changed?("192.168.0.0/24", "192.168.0.0/25", "")
end
end
end
end