I’ve got an interesting replecatable bug that has sprung up since I
migrated some code i’ve written from rails 1.2.3 to 2.0.2. I am using
a mysql backend database, in which I am storing IPv4 addresses as 32
bit unsigned integers. Of course, I don’t want my users to have to
enter the IP they are searching for in this fashion, nor do I want to
have to make the conversion myself every time I call the find_by_ip
function or find_or_create_by_ip function for the model, so I have
overloaded those two functions. find_by_ip now reads as follows:
def self.find_by_ip(ip)
super(NetAddr::CIDR.create(ip).to_i)
end
This works, the first time IP.find_by_ip(address) is called (this test
done in script/console):
ip = Ip.find_by_ip(“10.21.1.8”)
=> #<Ip id: 13, ip: 169148680>
However any subsequent calls to find_by_ip just return nil, even for
the same IP address, until the environment is reloaded:
reload!
Reloading…
=> trueip = Ip.find_by_ip(“10.21.1.8”)
=> #<Ip id: 13, ip: 169148680>ip = Ip.find_by_ip(“10.21.1.8”)
=> nil
If I add some puts statements in my overloaded find_by_ip, they never
get printed out after the first call to it has been done. Equally, if
I call find_by_ip with a 32 bit int form of an IPv4 address it works
reliably:
def self.find_by_ip(ip)
puts “Testing\n”
super(NetAddr::CIDR.create(ip).to_i)
end
?> reload!
Reloading…
=> true
ip = Ip.find_by_ip(“10.21.1.8”)
Testing
=> #<Ip id: 13, ip: 169148680>
ip = Ip.find_by_ip(“10.21.1.8”)
=> nilip = Ip.find_by_ip(169148680)
=> #<Ip id: 13, ip: 169148680>
It is as if, after the first call to my overloaded find_by_ip, rails
decides to ignore my overloaded function and go straight to the base
functionality it has for creating find_by functions. Can anyone else
test this to prove it’s not just me, and/or suggest who/where I should
report it as a bug?
Thanks
Dan Meyers
Network Support, Lancaster University