Skip to content

Commit 0be846c

Browse files
Merge pull request rails#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.
2 parents 1b6b1a9 + c395b99 commit 0be846c

File tree

2 files changed

+10
-1
lines changed
  • activerecord
    • lib/active_record/connection_adapters/postgresql/oid
    • test/cases/adapters/postgresql

2 files changed

+10
-1
lines changed

activerecord/lib/active_record/connection_adapters/postgresql/oid/cidr.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def serialize(value)
3131
# TODO: Remove when IPAddr#== compares IPAddr#prefix. See
3232
# https://github.com/ruby/ipaddr/issues/21
3333
def changed?(old_value, new_value, _new_value_before_type_cast)
34-
super || old_value.prefix != new_value.prefix
34+
super || !old_value.nil? && old_value.prefix != new_value.prefix
3535
end
3636

3737
def cast_value(value)

activerecord/test/cases/adapters/postgresql/cidr_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ class CidrTest < ActiveRecord::PostgreSQLTestCase
2121

2222
assert_equal "foo", type.serialize("foo")
2323
end
24+
25+
test "changed? with nil values" do
26+
type = OID::Cidr.new
27+
28+
assert_not type.changed?(nil, nil, "")
29+
assert type.changed?("192.168.0.0/24", nil, "")
30+
assert type.changed?(nil, "192.168.0.0/24", "")
31+
assert type.changed?("192.168.0.0/24", "192.168.0.0/25", "")
32+
end
2433
end
2534
end
2635
end

0 commit comments

Comments
 (0)