# Monkey Patching a Plugin

**URL:** <https://www.ruby-forum.com/t/monkey-patching-a-plugin/162576>\
**Category:** Rails\
**Created:** [March 11, 2009, 3:03pm UTC](https://www.ruby-forum.com/t/monkey-patching-a-plugin/162576 "2009-03-11T15:03:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@Daly](https://www.ruby-forum.com/u/Daly)\
**Post date:** [March 11, 2009, 3:03pm UTC](https://www.ruby-forum.com/t/monkey-patching-a-plugin/162576/1 "2009-03-11T15:03:38Z")

</div>

I am using a plugin that adds a method to ActiveRecord::Base. The  
added method is called self.create\_or\_update. I need to monkey patch  
this method. I tried in lib/ config/initializers and in  
environment.rb, but the one in the plugin is always the one that is  
run.

Where do I put my version of the method so that it overrides that of  
the plugin? I only use this method when running a rake task, so that  
may have a bearing on the issue?

Thanks,  
Ahmed

---

<div class="post-metadata">

**Author:** ![Daly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@Daly](https://www.ruby-forum.com/u/Daly)\
**Post date:** [March 11, 2009, 3:44pm UTC](https://www.ruby-forum.com/t/monkey-patching-a-plugin/162576/2 "2009-03-11T15:44:11Z")

</div>

On Mar 11, 2:03 pm, Daly [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am using a plugin that adds a method to ActiveRecord::Base. The  
> added method is called self.create\_or\_update. I need to monkey patch  
> this method. I tried in lib/ config/initializers and in  
> environment.rb, but the one in the plugin is always the one that is  
> run.

Showing us what you’ve tried would be a useful starting point.

Fred

---

<div class="post-metadata">

**Author:** ![Daly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@Daly](https://www.ruby-forum.com/u/Daly)\
**Post date:** [March 12, 2009, 12:56am UTC](https://www.ruby-forum.com/t/monkey-patching-a-plugin/162576/3 "2009-03-12T00:56:04Z")

</div>

Daly wrote:

> I am using a plugin that adds a method to ActiveRecord::Base. The  
> added method is called self.create\_or\_update. I need to monkey patch  
> this method. I tried in lib/ config/initializers and in  
> environment.rb, but the one in the plugin is always the one that is  
> run.
> 
> Where do I put my version of the method so that it overrides that of  
> the plugin? I only use this method when running a rake task, so that  
> may have a bearing on the issue?

Edit the plugin. It’s your source.

Submit what you did to its maintainer…

---

<div class="post-metadata">

**Author:** ![Daly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@Daly](https://www.ruby-forum.com/u/Daly)\
**Post date:** [March 11, 2009, 8:35pm UTC](https://www.ruby-forum.com/t/monkey-patching-a-plugin/162576/4 "2009-03-11T20:35:37Z")

</div>

The plugin is db-populate

in vendor/plugins/db-populate/lib/create\_or\_update.rb:  
class ActiveRecord::Base

# given a hash of attributes including the ID, look up the record by

ID.

# If it does not exist, it is created with the rest of the options.

# If it exists, it is updated with the given options.

# 

# Raises an exception if the record is invalid to ensure seed data

is loaded correctly.

# 

# Returns the record.

def self.create\_or\_update(options = {})  
id = options.delete(:id)  
record = find\_by\_id(id) || new  
record.id = id  
record.attributes = options  
record.save!

```
record

```

end  
end

I want to replace it with:

# db-populate monkey patch

# Patched to work even when the primary key != id

require ‘active\_record’  
class ActiveRecord::Base

# given a hash of attributes including the ID, look up the record by

ID.

# If it does not exist, it is created with the rest of the options.

# If it exists, it is updated with the given options.

# 

# Raises an exception if the record is invalid to ensure seed data

is loaded correctly.

# 

# Returns the record.

def self.create\_or\_update(options = {})  
# Need to get the primary key, can’t assume it’s always id  
id = options.delete(primary\_key.to\_sym)

```
record = send("find_by_#{primary_key}", id) || new
record.id = id
record.attributes = options
record.save!

record

```

end  
end

On Mar 11, 10:43 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Daly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@Daly](https://www.ruby-forum.com/u/Daly)\
**Post date:** [March 12, 2009, 4:10am UTC](https://www.ruby-forum.com/t/monkey-patching-a-plugin/162576/5 "2009-03-12T04:10:50Z")

</div>

I agree with this. Submit the patch. If he says no, fork the plugin  
yourself on github and maintain it. 🙂

---

<div class="post-metadata">

**Author:** ![Daly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@Daly](https://www.ruby-forum.com/u/Daly)\
**Post date:** [March 12, 2009, 4:45am UTC](https://www.ruby-forum.com/t/monkey-patching-a-plugin/162576/6 "2009-03-12T04:45:22Z")

</div>

Don’t monkey patch a monkey patch.

---

<div class="post-metadata">

**Author:** ![Daly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@Daly](https://www.ruby-forum.com/u/Daly)\
**Post date:** [March 12, 2009, 2:59pm UTC](https://www.ruby-forum.com/t/monkey-patching-a-plugin/162576/7 "2009-03-12T14:59:30Z")

</div>

Perfect! Thank you all 🙂

On Mar 11, 11:45 pm, Ranjeet J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
