Skip to content

Commit

Permalink
Merge pull request #51758 from taketo1113/fix-changed-cidr2
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
carlosantoniodasilva committed May 14, 2024
2 parents 1b6b1a9 + c395b99 commit 0be846c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def serialize(value)
# 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)
Expand Down
9 changes: 9 additions & 0 deletions activerecord/test/cases/adapters/postgresql/cidr_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ class CidrTest < ActiveRecord::PostgreSQLTestCase

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
Expand Down

0 comments on commit 0be846c

Please sign in to comment.