I have a table that amongst other things includes two columns,
“start_ip” and “end_ip”. Combined they express a range of IPs, say
127.0.0.0-127.0.0.100.
Does anyoone know i use find to match an ip to a certain range?
Something along the lines of:
ip = “127.0.0.3”
ips = SwedishIp.find_by_something(ip)
So I’ve converted the IPs to integers using IPAddr, which makes it a
whole lot easier to see if an incoming IP fits a certain range.
The code I’m looking at now is:
@request_ip = IPAddr.new(request.remote_ip)
ips = IpRanges.find(:all)
ips.each do |ip|
range = ip.start_ip_integer…ip.end_ip_integer
if range.include?(@request_ip.to_i) @ip_range = ip
end
end
render :text => @ip_range.group
Looks painfully slow to iterate thru every row like that - and I keep it
all in my controller when I assume it should be refactored into my
IpRange-model?