Hi! 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) Any help is most appreciated. Thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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? Any way to speed things up? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Probably the way to speed it up the most is with SQL. Something like this might work: IpRanges.find(:all, :conditions => [''start_ip_integer <= :ip AND end_ip_integer >= :ip'', {:ip => @request_ip.to_i}]) That would probably be in a class method in your model. I hope that helps, Paul --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---