On 1/19/08, Mark Wilden <[EMAIL PROTECTED]> wrote:
>
> From: [email protected]
> [mailto:[EMAIL PROTECTED] On Behalf Of Rick DeNatale
>
> >It has to be overwriting the definition in his subclass of AR::Base,
> >otherwise he wouldn't have seen the problem.
>
> Let me see if I got this straight.
>
> 1) Model#find_x is defined
> 2) When find_x is called, it calls super, which is AR::Base#find_x.
> 3) AR::Base has no such method, so method_missing is called in the context
> of Model (not AR::Base)
> 4) method_missing generates Model#find_x
>
> So what's weird is that method_missing is adding a method to the subclass
> that already exists, just because the method can't be found in the
> superclass.
>
> Am I understanding this right?

Yes,   here's a synopsis of the code from
ActiveRecord::Base#method_missing in ActiveRecord 2.0.2, I've replaced
some code with #... to make it shorter and more obvious.

module ActiveRecord
  #...
  class Base
    #...
    class << self # Class methods
     # ...
        def method_missing(method_id, *arguments)
          if match = /^find_(all_by|by)_([_a-zA-Z]\w*)$/.match(method_id.to_s)
            # ...
            self.class_eval %{
              def self.#{method_id}(*args)
                # body of generated method elided for this post
              end
            }, __FILE__, __LINE__
            send(method_id, *arguments)
          elsif match =
/^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/.match(method_id.to_s)
            # ...
            self.class_eval %{
              def self.#{method_id}(*args)
                # Body of method elided for this post
              end
            }, __FILE__, __LINE__
            send(method_id, *arguments)
          else
            super
          end
        end
     #...
     end
end

in those two self.class_eval calls, self will be the subclass of
ActiveRecord::Base and that's where the method will get defined
overwriting the existing method, which got to method_missing by
calling super.


-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to