Vinicius Cubas Brand
2006-Nov-09 13:24 UTC
redefining the method belongs_to in ActiveRecord::Base
Hi. I''ve tried to redefine the method belongs_to in ActiveRecord::Base, so every time a model is defined as belonging to :attachment, it should have some additional methods. I tried to do an alias of belongs_to, but this was not successful. The error message in Webrick was> Booting WEBrick..../script/../config/../lib/ActiveRecordExtensions.rb:67: undefined method `belongs_to'' for module `ActiveRecord'' (NameError) The code with problem is: alias :attachment_belongs_to :belongs_to def belongs_to(association_id, options={}) self.attachment_belongs_to(association_id,options) if association_id == :attachment code = <<EOS alias :tmp_attach= :attachment def attachment=(content) if content.class.name == ''Attachment'' #old functionality self.tmp_attach=(content) else if content.class.name.match(/^Hash/) && content["content"] && content["content"].original_filename != "" if self.attachment self.attachment.update_attributes(content) else attach = Attachment.new(content) attach.save self.tmp_attach=(attach) end end end end alias :tmp_destroy :destroy def destroy attach = self.attachment self.tmp_destroy attach.destroy if attach end EOS module_eval(code) end end --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Nov-09 13:29 UTC
Re: redefining the method belongs_to in ActiveRecord::Base
Hi -- On Thu, 9 Nov 2006, Vinicius Cubas Brand wrote:> > Hi. I''ve tried to redefine the method belongs_to in ActiveRecord::Base, > so every time a model is defined as belonging to :attachment, it should > have some additional methods. I tried to do an alias of belongs_to, but > this was not successful. The error message in Webrick was > >> Booting WEBrick... > ./script/../config/../lib/ActiveRecordExtensions.rb:67: undefined > method `belongs_to'' for module `ActiveRecord'' (NameError) > > The code with problem is: > > alias :attachment_belongs_to :belongs_toI suspect the problem was actually before this. It sounds like you had: module ActiveRecord when you wanted: class ActiveRecord::Base (I haven''t looked at the rest of the code in detail but I think that''s what''s triggering this error.) David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Alex Wayne
2006-Nov-09 16:52 UTC
Re: redefining the method belongs_to in ActiveRecord::Base
unknown wrote:> Hi -- > > On Thu, 9 Nov 2006, Vinicius Cubas Brand wrote: > >> The code with problem is: >> >> alias :attachment_belongs_to :belongs_to > > I suspect the problem was actually before this. It sounds like you > had: > > module ActiveRecord > > when you wanted: > > class ActiveRecord::Base > > (I haven''t looked at the rest of the code in detail but I think that''s > what''s triggering this error.)I suspect this is correct. Also belongs_to is a class method and must be declared as such. You can also define you own class that enscapsulates this through inheritance, which has the advantage of calling to original functionality through a clean ans slick call to "super" class Foo < ActiveRecord::Base self.abstract_class = true # tell AR there is no table for this class def self.belongs_to(*args) if args[0] == ''something'' super(*args) # do whatever AR does normally else do_something_custom end end end class MyModel < Foo belongs_to :bar end In my experience inheritance is the cleanest way to extend an existing complex class with non standard functionality and just feels more right than some method aliasing hack around a method. -- 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 -~----------~----~----~----~------~----~------~--~---