# Overwrite ActiveRecord gem function in my rails app

**URL:** <https://www.ruby-forum.com/t/overwrite-activerecord-gem-function-in-my-rails-app/115304>\
**Category:** Rails\
**Created:** [September 23, 2007, 3:10pm UTC](https://www.ruby-forum.com/t/overwrite-activerecord-gem-function-in-my-rails-app/115304 "2007-09-23T15:10:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 23, 2007, 3:10pm UTC](https://www.ruby-forum.com/t/overwrite-activerecord-gem-function-in-my-rails-app/115304/1 "2007-09-23T15:10:28Z")

</div>

Hi,

I would like to overwrite the function “quote” in  
ActiveRecord::ConnectionAdapters::Quoting for my whole rails  
application.

Unfortunelty, so far I was nt successfull. Maybe somebody can point me  
in the right direction?

–  
Volker

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 23, 2007, 3:59pm UTC](https://www.ruby-forum.com/t/overwrite-activerecord-gem-function-in-my-rails-app/115304/2 "2007-09-23T15:59:40Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Hi,
> 
> I would like to overwrite the function “quote” in  
> ActiveRecord::ConnectionAdapters::Quoting for my whole rails  
> application.
> 
> Unfortunelty, so far I was nt successfull. Maybe somebody can point me  
> in the right direction?
> 
> –  
> Volker

Yeah,  
I think you just want to overwrite it for the specific connection  
adapter.  
Do “ActiveRecord::Base.connection.class”,

reopen that class,  
and redefine quote on that class.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 23, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/overwrite-activerecord-gem-function-in-my-rails-app/115304/3 "2007-09-23T19:57:32Z")

</div>

See also alias\_method\_chain if you want to overwrite but still call  
through to the original function

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 23, 2007, 9:45pm UTC](https://www.ruby-forum.com/t/overwrite-activerecord-gem-function-in-my-rails-app/115304/4 "2007-09-23T21:45:37Z")

</div>

Frederick C. wrote:

> See also alias\_method\_chain if you want to overwrite but still call  
> through to the original function
> 
> Fred  
> Indeed Freddy,

Although I quite like doing my aliases as “blah\_before\_blah” and  
“blah\_after\_blah”  
rather than with / without.  
but…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 24, 2007, 9:54am UTC](https://www.ruby-forum.com/t/overwrite-activerecord-gem-function-in-my-rails-app/115304/5 "2007-09-24T09:54:04Z")

</div>

Hi,

Ok, after some further try and errors…  
I ve achieved what I want with the following code:  
module ActiveRecord  
module ConnectionAdapters # :nodoc:  
class AbstractAdapter  
# Quotes the column value to help prevent  
# {SQL injection attacks}[[Wikipedia, the free encyclopedia](http://en.wikipedia.org/wiki/)  
SQL\_injection].  
def quote(value, column = nil)

As suggested, I ve tried this time to overwrite the AbstractAdapter  
class and not the quoting module.

Thanks a lot.

–  
Volker

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 24, 2007, 11:59am UTC](https://www.ruby-forum.com/t/overwrite-activerecord-gem-function-in-my-rails-app/115304/6 "2007-09-24T11:59:23Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Hi,
> 
> Ok, after some further try and errors…  
> I ve achieved what I want with the following code:  
> module ActiveRecord  
> module ConnectionAdapters # :nodoc:  
> class AbstractAdapter  
> # Quotes the column value to help prevent  
> # {SQL injection attacks}[[Wikipedia, the free encyclopedia](http://en.wikipedia.org/wiki/)  
> SQL\_injection].  
> def quote(value, column = nil)
> 
> As suggested, I ve tried this time to overwrite the AbstractAdapter  
> class and not the quoting module.
> 
> Thanks a lot.
> 
> –  
> Volker

hmm…  
well I tried overwriting the Quoting module,  
and it works.

> > module ActiveRecord::ConnectionAdapters::Quoting  
> > def quote(value, column=nil)  
> > return “quoting module”  
> > end  
> > end

so perhaps you were doing something else wrong.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 24, 2007, 9:39am UTC](https://www.ruby-forum.com/t/overwrite-activerecord-gem-function-in-my-rails-app/115304/7 "2007-09-24T09:39:01Z")

</div>

Hi,

The theory I ve understood…  
Unfortunelty, ive got no idea how to actually do it.

I ve tried the following:  
I ve created a quoting.rb in my rails app lib folder.  
module ActiveRecord  
module ConnectionAdapters # :nodoc:  
module Quoting  
def quote(value, column = nil)  
my new code…

Afterwards I ve added a require of my little quoting file into my  
environment file… well, but that does not work  
So, you mentioned that just reopen the class and …  
How do I actually do that?

–  
Volker
