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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 11, 2:03 pm, Daly <aeld...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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> > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mar 11, 2:03 pm, Daly <aeld...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I agree with this. Submit the patch. If he says no, fork the plugin yourself on github and maintain it. :) On Wed, Mar 11, 2009 at 6:50 PM, Phlip <phlip2005-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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... > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Don''t monkey patch a monkey patch. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Perfect! Thank you all :) On Mar 11, 11:45 pm, Ranjeet Jones <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Don''t monkey patch a monkey patch. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---