Encoded Columns and ActiveRecord

Peer Allan
Fri, 01 Jun 2007 20:47:14 -0000

Hi all,

I am trying store IP addresses in a database.  In my old PHP days when I
wrote the SQL i used the INET_ATON and INET_NTOA functions in MySQL to
to the conversions of the address when I inserted or pulled the rows
from the database.  I would like to do something similar with my
ActiveRecord model in Rails as storing the IP as an integer is much more
efficient than in its dot format.

My question is how do I do it?  Is there a way for me to setup my model
so that whenever it saves it converts the stored IP and when it finds a
record it automatically converts it back to the dot format?  Is there a
way I can use the MySQL functions?  Or could I just always keep it in
the integer state, but have accessors in the class convert it when I
access it?

Thanks