简体   繁体   中英

convert this sql statement to rails

I have the following sql statement

SELECT * FROM geo_blocks 
WHERE index_geo = INET_ATON("92.229.175.253")-(INET_ATON("92.229.175.253")%65536) 
AND INET_ATON("92.229.175.253") 
BETWEEN ip_start AND ip_end;

How would I convert this to a rails format with something like this?

geodata = GeoBlocks.where(.......)

edit

This is what I ended up doing in the end.

require 'ipaddr'
ip = IPAddr.new("92.229.175.253")
geodata = GeoBlocks.where({:index_geo => (ip.to_i - (ip.to_i%(65536)))}, {:ip_start.lt => ip.to_i, :ip_end.gt => ip.to_i})

This should work:

geodata = GeoBlocks.where(
  "index_geo = INET_ATON(:ip)-(INET_ATON(:ip)%65536) AND INET_ATON(:ip) BETWEEN :ip_start AND :ip_end", {
    :ip        => "92.229.175.253",
    :ip_start  => ip_start,
    :ip_end    => ip_end
  }
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM