将此sql语句转换为rails

时间:2011-08-13 14:44:19

标签: sql ruby-on-rails

我有以下sql语句

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;

如何将此转换为具有类似内容的rails格式?

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

修改

这就是我最终做的事情。

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})

1 个答案:

答案 0 :(得分:0)

这应该有效:

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
  }
)