# Problems overriding module with plugin

**URL:** <https://www.ruby-forum.com/t/problems-overriding-module-with-plugin/52091>\
**Category:** Rails\
**Created:** [January 10, 2006, 9:08pm UTC](https://www.ruby-forum.com/t/problems-overriding-module-with-plugin/52091 "2006-01-10T21:08:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![M\_Daggett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@M\_Daggett](https://www.ruby-forum.com/u/M_Daggett)\
**Post date:** [January 10, 2006, 9:08pm UTC](https://www.ruby-forum.com/t/problems-overriding-module-with-plugin/52091/1 "2006-01-10T21:08:00Z")

</div>

Hi I am trying to create a plug-in to fix the error in the rails core  
produced by the multiple delete on a HABTM relationship. I have  
confirmed that my plug-in is being included into the base during  
runtime however the code does not seem to be overridding the base  
class.

module ActiveRecord  
module Associations  
module ClassMethods  
def has\_and\_belongs\_to\_many(association\_id, options = {},  
&extension)  
…  
code  
…  
end  
end  
end  
end

this is what i have in my vendor/plugins/patches/lib directory, does  
anyone know why this is not working?  
Thanks,  
Mark

–

---

<div class="post-metadata">

**Author:** ![M\_Daggett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@M\_Daggett](https://www.ruby-forum.com/u/M_Daggett)\
**Post date:** [January 10, 2006, 11:23pm UTC](https://www.ruby-forum.com/t/problems-overriding-module-with-plugin/52091/2 "2006-01-10T23:23:09Z")

</div>

On Jan 10, 2006, at 12:07 PM, M Daggett wrote:

> &extension)  
> anyone know why this is not working?  
> Thanks,  
> Mark

Mark-

```
It looks like you are on the right track but you still need the

```

self.included part. The basic pattern for this as I know it is like so:

module EZ  
def self.included(base)  
base.extend(ClassMethods)  
end  
module ClassMethods  
def something(\*args, &block)  
end  
end  
end

Then in your init.rb file you would do this:

ActiveRecord::Base.send :include, EZ

```
Of course alter that for wherever you are trying to get your module

```

included into.

Cheers-

-Ezra Z.  
Yakima Herald-Republic  
WebMaster  
[http://yakimaherald.com](http://yakimaherald.com)  
509-577-7732  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![M\_Daggett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@M\_Daggett](https://www.ruby-forum.com/u/M_Daggett)\
**Post date:** [January 11, 2006, 2:06am UTC](https://www.ruby-forum.com/t/problems-overriding-module-with-plugin/52091/3 "2006-01-11T02:06:05Z")

</div>

Hi Ezra,  
I was unsucessful in getting your suggestions to work. Here is what my  
files look like. I picked a small patch the fact that you need to tell  
mysql to ignore foreign key when loading and unloading the fixture  
when running tests.

my plug-in looks like this:

module MysqlIgnoreForeignKeysOnFixtureLoading  
def self.included(base)  
base.extend(Fixtures)  
end  
module Fixtures  
def delete\_existing\_fixtures  
# @connection.delete “DELETE FROM #{@table\_name}”, ‘Fixture  
Delete’  
# Deactivate foreign keys with MySQL to prevent errors when  
clearing tables  
# with self referencing foreign keys.  
if @connection.kind\_of?  
ActiveRecord::ConnectionAdapters::MysqlAdapter  
@connection.update “SET FOREIGN\_KEY\_CHECKS = 0”, ‘Fixtures  
deactivate foreign key checks.’;  
@connection.update “DELETE FROM #{@table\_name}”, ‘Fixture  
Delete’;  
@connection.update “SET FOREIGN\_KEY\_CHECKS = 1”, ‘Fixtures  
activate foreign key checks.’;  
else  
@connection.delete “DELETE FROM #{@table\_name}”, ‘Fixture  
Delete’  
end  
end  
end  
end

My init file looks like so:

require ‘mysql\_adapter\_patches’  
ActiveRecord::Base.send :include, MysqlIgnoreForeignKeysOnFixtureLoading

when I run rake it blows up on all the foreign keys so I know that it  
is not picking up the errors. Do you see something wrong with the  
syntax?

Thanks,  
Mark

On 1/10/06, Ezra Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > module Associations
> 
> self.included part. The basic pattern for this as I know it is like so:
> 
> Cheers-
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

---

<div class="post-metadata">

**Author:** ![M\_Daggett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@M\_Daggett](https://www.ruby-forum.com/u/M_Daggett)\
**Post date:** [January 11, 2006, 2:57am UTC](https://www.ruby-forum.com/t/problems-overriding-module-with-plugin/52091/4 "2006-01-11T02:57:16Z")

</div>

Mark-

```
It looks ok to me. Although I am not sure if AR::Base is the right

```

place to include the module. What class are you trying to extend  
specifically? Also have a look at the plugins page on the wiki. It  
has a plugin that will apply patches for you that might help.

Cheers-  
-EZra

On Jan 10, 2006, at 5:05 PM, M Daggett wrote:

> ```
> base.extend(Fixtures)
> @connection.update "SET FOREIGN_KEY_CHECKS = 0", 'Fixtures
> 
> ```
> 
> end
> 
> > > end
> > 
> > ```
> > def something(*args, &block)
> > Of course alter that for wherever you are trying to get  
> > 
> > ```
> > 
> > 509-577-7732
> 
> –
> 
> * * *
> 
> I am Mark Daggett and I approve this message.
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![M\_Daggett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@M\_Daggett](https://www.ruby-forum.com/u/M_Daggett)\
**Post date:** [January 12, 2006, 7:15am UTC](https://www.ruby-forum.com/t/problems-overriding-module-with-plugin/52091/5 "2006-01-12T07:15:10Z")

</div>

Hi EZra,  
Thanks for writing back.  
I looked around in the plug-in directory but could not find  
specifically any plug-in that facilitates the patching of the rail’s  
core. Do you have a name of said plug-in?

In regards to the plug-in that I am trying to write, I would like to  
patch:

ActiveRecord/fixtures.rb

And

ActiveRecord/associations.rb

I am not too sure how that plays out in the code-base in terms of  
inheritance etc.

Any help greatly appreciated.  
Best,  
Mark

On 1/10/06, Ezra Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Jan 10, 2006, at 5:05 PM, M Daggett wrote:
> 
> > def self.included(base)  
> > ActiveRecord::ConnectionAdapters::MysqlAdapter  
> > end
> > 
> > > > class.  
> > > > end  
> > > > Mark-  
> > > > module ClassMethods
> > > 
> > > [http://yakimaherald.com](http://yakimaherald.com)
> 
> -Ezra Z.  
> WebMaster  
> Yakima Herald-Republic Newspaper  
> removed\_email\_address@domain.invalid  
> 509-577-7732
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

---

<div class="post-metadata">

**Author:** ![M\_Daggett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@M\_Daggett](https://www.ruby-forum.com/u/M_Daggett)\
**Post date:** [January 12, 2006, 6:53pm UTC](https://www.ruby-forum.com/t/problems-overriding-module-with-plugin/52091/6 "2006-01-12T18:53:12Z")

</div>

Mark-

```
It looks like that plugin isn't on the plugins page anymore. Not

```

sure why. I think your best bet would be to find a few plugins and  
look at the way they work to get themselves included into the right  
place. I am not sure what exactly you want to extend so I don’t know  
what to tell you at this point.

Cheers-  
-Ezra

On Jan 11, 2006, at 10:11 PM, M Daggett wrote:

> > -EZra
> > 
> > > ```
> > > # Deactivate foreign keys with MySQL to prevent errors when
> > > else
> > > 
> > > ```
> > > 
> > > Thanks,
> > > 
> > > > > Hi I am trying to create a plug-in to fix the error in the rails  
> > > > > &extension)  
> > > > > does  
> > > > > like so:
> > > 
> > > I am Mark Daggett and I approve this message.  
> > > 509-577-7732  
> > > I am Mark Daggett and I approve this message.
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

-Ezra Z.  
Yakima Herald-Republic  
WebMaster  
[http://yakimaherald.com](http://yakimaherald.com)  
509-577-7732  
removed\_email\_address@domain.invalid
