method_missing的倍率不工作 [英] method_missing override is not working

查看:151
本文介绍了method_missing的倍率不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个方便的ActiveRecord扩展,授方法基本对象(基于多-table继承

I wrote a convenience ActiveRecord extension to delegate methods to a base object (based on multi-table inheritance)


class ActiveRecord::Base
  def self.acts_as(base)
    class_eval %Q{
      def method_missing(method, *args, &blk)
        #{base}.send(method, *args, &blk)
      rescue NoMethodError
        super
      end
    }
  end
end

我有一个状态类和基础


# state class
class MyState < ActiveRecord::Base
  belongs_to :my_object
  acts_as :my_object
end

# base class
class MyObject < ActiveRecord::Base
  has_one :head, :class_name => 'MyState'
  has_one :tail, :class_name => 'MyState'
end

当我尝试这样做了,我发现它并不在某些情况下工作。更具体地,

When I tried this out, I found out that it doesn't work in some cases. More specifically,


> MyState.first.some_method_in_base
nil
> MyObject.first.tail.some_method_in_base
NoMethodError: undefined method `some_method_in_base' for #<ActiveRecord::Associations::HasOneAssociation:0xABCDEFG>

谁能开导我,为什么一件作品,另一个不?

Can anyone enlighten me as to why one works and the other doesn't?

推荐答案

当您运行 MyObject.first.tail 实际响应的对象是<一个href="http://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/association_proxy.rb">AssociationProxy类

When you run MyObject.first.tail the object that actually responds is an AssociationProxy class that

已经大部分基础的实例方法去除,和代表       #未知的方法,通过以@target   method_missing的

has most of the basic instance methods removed, and delegates # unknown methods to @target via method_missing

您可以获取有关代理运行的详细信息:

You can get more details about the proxy running:

MyObject.first.proxy_owner
MyObject.first.proxy_reflection
MyObject.first.proxy_target

如果你看一下在code,你可以看到AssociationProxy代理的方法some_method_in_base您MyState类的仅在 MyState 的响应* some_method_in_base * 作为你可以在下面的code看到的。

If you look in the code, you can see that the AssociationProxy proxies the method some_method_in_base to your MyState class only if MyState responds to *some_method_in_base* as you can see in the code below.

  private
    # Forwards any missing method call to the \target.
    def method_missing(method, *args, &block)
      if load_target
        if @target.respond_to?(method)
          @target.send(method, *args, &block)
        else
          super
        end
      end
    end

因此​​,的method_missing 您在目标类定义的永远不会被调用。

Therefore, the method_missing you have defined in the target class is never called.

这篇关于method_missing的倍率不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆