我有一个包含7000条低和高ip地址记录的数据库表,我正在使用RailsIPAaddr
类。
如果我的request.remote_ip
在表的low
或high
字段中被发现,我得到low
和high
,并将它们从查询中放入IPAddr
,然后运行它,我得到true
。现在我被问到,如果remote_ip
地址在表中不存在,但它属于表中的低ip地址范围和高ip地址范围,也可以得到true
。
如有任何意见,我将不胜感激。
使用inet
内置的Postgres type:
Thing.where(
"inet ? << any(array[things.low, things.high]::inet[])", ip_address
)
测试传递的ip地址是否包含在上界和下界内。inet
函数强制转换为inet类型。Ruby的对应值是:
IpAddr.new(low)..IpAddr.new(high).cover?(IpAddr.new(ip_address))