Rails:使用关联时,不会在模型上调用method_missing
||
我当前的代码:
class Product < ActiveRecord::Base
belongs_to :category
end
class Category < ActiveRecord::Base
def method_missing name
true
end
end
Category.new.ex_undefined_method #=> true
Product.last.category.ex_undefined_method #=> NoMethodError: undefined method `ex_undefined_method\' for #<ActiveRecord::Associations::BelongsToAssociation:0xc4cd52c>
发生这种情况是因为Rails中的该代码仅将存在的方法传递给模型。
private
def method_missing(method, *args)
if load_target
if @target.respond_to?(method)
if block_given?
@target.send(method, *args) { |*block_args| yield(*block_args) }
else
@target.send(method, *args)
end
else
super
end
end
end
这就是我要的:
Product.last.category.ex_undefined_method #=> true
我该怎么做?
没有找到相关结果
已邀请:
3 个回复
辟官陡板休
:
实际上,如果您重新定义
,则应该始终更新
-您已经更改了班级的界面,因此需要确保每个人都知道它。看这里。
俺呵誓放胳
芜任亮蜡
通过
作为AssociationProxy的猴子补丁