Hello Everyone
I''m trying to overwrite a default accessor in my model to use the
IPAddr class
to convert a 4-byte string stored in the database to a human-readable IP
address. Like this:
class Host < ActiveRecord::Base
def ip_address
IPAddr.new_ntoh(read_attribute("ip_address"))
end
# ...other stuff
end
In my templates I use things like
<%= @host.ip_address %>
to get a nice dotted quad representation of the IP address.
But when I use
<%= text_field ''host'', ''ip_address'' %>
in a form, to be able to edit the addresses of my hosts, Rails doesn''t
seem to
be using my accessor, and instead produces a four character string. I could of
course type the HTML for the input field and use the method I know is working,
but that would be like cheating, wouldn''t it?
Asking for help isn''t cheating, hence this post. Any help is most
welcome.
More details:
* In MySQL, the ip_address field is a varchar(4) (even though I created it as
a char(4)).
* I''m running Rails 0.11.1 with Ruby 1.8.2 on Debian Sarge.
-martin