Monkey Patching a Plugin

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

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

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.

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

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

Perfect! Thank you all :slight_smile: