skip to Main Content

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?

2

Answers


  1. 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 where address is an IPv4 or IPv6 address and y 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.

    Login or Signup to reply.
  2. string works as well.

    # Migration
    t.string   :current_sign_in_ip
    
    # Controller
    user.current_sign_in_ip = request.remote_ip
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search