Skip to content
ActiveRecord extension to save IPs as ints, but get and set them as dotted decimal strings
Ruby
Pull request Compare This branch is 10 commits behind apeckham:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
test
MIT-LICENSE
README
Rakefile
init.rb

README

This code helps you store IP addresses as ints in the database.

Create a column on your ActiveRecord model to store IP addresses. In my app, that was a migration like this:

add_column :my_models, :ipaddress, :integer, :limit => 8
(Make sure the column is big enough to store an IP)

Then in your model, use has_ip_column:
has_ip_column :ipaddress

On instances of MyModel, you can now set ipaddress as a dotted decimal string:
MyModel.new(:ipaddress => "201.102.201.102")

When you save the model, it's stored as an integer in the database.
Something went wrong with that request. Please try again.