PostgreSQL支持inet数据类型。使用正确的数据类型,许多问题就会消失。
scratch=# create table test (
scratch(# ip_addr inet not null);
CREATE TABLE
scratch=# insert into test values ('147.226.211.40');
INSERT 0 1
scratch=# insert into test values ('1.39.80.12');
INSERT 0 1
scratch=# insert into test values ('128.237.199.43');
INSERT 0 1
scratch=# select * from test order by ip_addr;
ip_addr
----------------
1.39.80.12
128.237.199.43
147.226.211.40
(3 rows)
ActiveRecord支持INET数据类型。简单来说 。 。 。
$ bin/rails generate scaffold IpAddr ip_addr:inet
编辑控制器。订购编号:ip_addr
。
$ head -9 app/controllers/ip_addrs_controller.rb
class IpAddrsController < ApplicationController
before_action :set_ip_addr, only: [:show, :edit, :update, :destroy]
# GET /ip_addrs
# GET /ip_addrs.json
def index
@ip_addrs = IpAddr.all.order(:ip_addr)
# ^^^^^^^^^^^^^^^
end
浏览到该页面,您会发现IP地址的排序正确。
IPv4地址实际上可以存储为32位整数。我想知道'1.39.80。,12.97'是什么。 – 2015-02-10 18:53:24
这是我的错误,当提出问题。 – Kashiftufail 2015-02-10 18:58:42
实际上,IPv4地址*是一个32位整数。 – 2015-02-11 22:28:38