Ruby Rails从db表的low和high字段中查找ip地址



我有一个包含7000条低和高ip地址记录的数据库表,我正在使用RailsIPAaddr类。

如果我的request.remote_ip在表的lowhigh字段中被发现,我得到lowhigh,并将它们从查询中放入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))

最新更新