I have a requirement where I need to store an IP address in the table on a rails app. The format could be of IPv4 or IPv6. Currently I thought of storing them as 'strings'. But is this a good approach. Could there be a better alternative here that could accomodate both?
CodePudding user response:
The appropriate PostgreSQL data type is clearly inet
.
While inet
can also represent a subnet, it is also the appropriate type for a single IP address:
The input format for this type is
address/y
whereaddress
is an IPv4 or IPv6 address andy
is the number of bits in the netmask. If the/y
portion is omitted, the netmask is taken to be 32 for IPv4 or 128 for IPv6, so the value represents just a single host.
CodePudding user response:
string
works as well.
# Migration
t.string :current_sign_in_ip
# Controller
user.current_sign_in_ip = request.remote_ip