# Bug in Rails 2.0 when overloading a find\_by method?

**URL:** <https://www.ruby-forum.com/t/bug-in-rails-2-0-when-overloading-a-find-by-method/196575>\
**Category:** Ruby\
**Created:** [January 16, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/bug-in-rails-2-0-when-overloading-a-find-by-method/196575 "2008-01-16T16:10:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![carr0t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8e8cbc/32.png) [@carr0t](https://www.ruby-forum.com/u/carr0t)\
**Post date:** [January 16, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/bug-in-rails-2-0-when-overloading-a-find-by-method/196575/1 "2008-01-16T16:10:17Z")

</div>

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…  
> > =\> true
> 
> > ip = 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”)  
> > =\> nil
> 
> > ip = 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

---

<div class="post-metadata">

**Author:** ![carr0t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8e8cbc/32.png) [@carr0t](https://www.ruby-forum.com/u/carr0t)\
**Post date:** [January 16, 2008, 7:47pm UTC](https://www.ruby-forum.com/t/bug-in-rails-2-0-when-overloading-a-find-by-method/196575/2 "2008-01-16T19:47:29Z")

</div>

On Jan 16, 2008, at 07:06 , Carr0t wrote:

> 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.

Off topic. Send to the ruby on rails list.
