# Override method in ActiveRecord

**URL:** <https://www.ruby-forum.com/t/override-method-in-activerecord/102199>\
**Category:** Rails\
**Created:** [August 12, 2006, 5:38pm UTC](https://www.ruby-forum.com/t/override-method-in-activerecord/102199 "2006-08-12T17:38:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kronidas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e79b87/32.png) [@kronidas](https://www.ruby-forum.com/u/kronidas)\
**Post date:** [August 12, 2006, 5:38pm UTC](https://www.ruby-forum.com/t/override-method-in-activerecord/102199/1 "2006-08-12T17:38:24Z")

</div>

Hello,  
there is a method ActiveRecord::Base#exists? . I would like to override  
this method in a class ActiveRecord.

Could anyone tell me how to do it?

TIA

---

<div class="post-metadata">

**Author:** ![kronidas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e79b87/32.png) [@kronidas](https://www.ruby-forum.com/u/kronidas)\
**Post date:** [August 13, 2006, 12:11am UTC](https://www.ruby-forum.com/t/override-method-in-activerecord/102199/2 "2006-08-13T00:11:04Z")

</div>

Firstname Secondname wrote:

> Hello,  
> there is a method ActiveRecord::Base#exists? . I would like to override  
> this method in a class ActiveRecord.
> 
> Could anyone tell me how to do it?
> 
> TIA

I can easily extend ApplicationController. How do I extend ActiveRecord?

---

<div class="post-metadata">

**Author:** ![kronidas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e79b87/32.png) [@kronidas](https://www.ruby-forum.com/u/kronidas)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/override-method-in-activerecord/102199/3 "2006-08-15T14:18:06Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Firstname Secondname wrote:

> Hello,  
> there is a method ActiveRecord::Base#exists? . I would like to override  
> this method in a class ActiveRecord.
> 
> Could anyone tell me how to do it?

Before you override this, why do you want to override this? Perhaps  
there is a better way to achieve what you want rather then changing the  
behavior of exists?

Zach  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2.2 (GNU/Linux)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iD8DBQFE3f4fMyx0fW1d8G0RAmxZAJ93g65XkKWLonznEjuBy9aDg4185QCdFMJL  
ml5xsC6RWOxxik5Fi4rskvo=  
=y3IH  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![kronidas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e79b87/32.png) [@kronidas](https://www.ruby-forum.com/u/kronidas)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/override-method-in-activerecord/102199/4 "2006-08-15T14:18:07Z")

</div>

Hello ‘firstname’,  
Well, since Rails is, at its heart, jst ‘good old ruby’, jst  
override the class method the same way you would in ruby. Eg;

> > print Booking.exists?(153153)  
> > true=\> nil
> 
> > class Booking  
> > def Booking.exists?(str)  
> > puts " over-ridden method "  
> > super(str)  
> > end  
> > end  
> > =\> nil
> 
> > print Booking.exists?(153153)  
> > over-ridden method  
> > true=\> nil

```
Of course, if your over-ridding a class that lots of other classes

```

inherit from (eg; ActiveRecord::Base) you may jst want to actually  
over-ride the method in the class where you need it. I strongly doubt  
you need to over-ride the -all- the descendants of Base… but… your  
choice 😉

```
Regards
Stef
```

---

<div class="post-metadata">

**Author:** ![kronidas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e79b87/32.png) [@kronidas](https://www.ruby-forum.com/u/kronidas)\
**Post date:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/override-method-in-activerecord/102199/5 "2006-08-15T14:19:29Z")

</div>

class ActiveRecord::Base

# your code here

end

-Jonathan.

---

<div class="post-metadata">

**Author:** ![kronidas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e79b87/32.png) [@kronidas](https://www.ruby-forum.com/u/kronidas)\
**Post date:** [August 15, 2006, 3:57pm UTC](https://www.ruby-forum.com/t/override-method-in-activerecord/102199/6 "2006-08-15T15:57:43Z")

</div>

Firstname Secondname wrote:

> Firstname Secondname wrote:
> 
> > Hello,  
> > there is a method ActiveRecord::Base#exists? . I would like to override  
> > this method in a class ActiveRecord.
> > 
> > Could anyone tell me how to do it?
> > 
> > TIA
> 
> I can easily extend ApplicationController. How do I extend ActiveRecord?

Could you provide skeleton file (.rb) and tell me where to put it so  
rails could pick it up and override method exists? in ActiveRecord::Base  
.

SELECT COUNT(_) FROM table WHERE id = ? is slower than SELECT COUNT(_)  
FROM table WHERE id = ? LIMIT 1

I would like to use my own solution with exists? method.

---

<div class="post-metadata">

**Author:** ![kronidas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e79b87/32.png) [@kronidas](https://www.ruby-forum.com/u/kronidas)\
**Post date:** [August 15, 2006, 7:42pm UTC](https://www.ruby-forum.com/t/override-method-in-activerecord/102199/7 "2006-08-15T19:42:13Z")

</div>

The exists? method already applies the LIMIT 1 by using find(:first,  
…).

Maybe I am misunderstanding your problem?

-Gregory Gross

---

<div class="post-metadata">

**Author:** ![kronidas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e79b87/32.png) [@kronidas](https://www.ruby-forum.com/u/kronidas)\
**Post date:** [August 15, 2006, 8:14pm UTC](https://www.ruby-forum.com/t/override-method-in-activerecord/102199/8 "2006-08-15T20:14:54Z")

</div>

Greg Gross wrote:

> The exists? method already applies the LIMIT 1 by using find(:first,  
> …).
> 
> Maybe I am misunderstanding your problem?
> 
> -Gregory Gross

You are right.  
The problem is solved.

Thank you
