简体   繁体   English

将此 sql 语句转换为 rails

[英]convert this sql statement to rails

I have the following sql statement我有以下 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;

How would I convert this to a rails format with something like this?我将如何将其转换为具有类似这样的 Rails 格式?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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