查找 class 方法被委托给? (Rails)
Look up the class a method was delegated to? (Rails)
如果我用delegate :Mayfield, to: :user
我可以在 :Mayfield 上调用什么来查看它是否已委托给 :user class?
解决方案:
不可能
正如@BKSpurgeon 已经指出的那样,delegate
方法的 implementation 除了创建一个在对象上调用给定方法的方法之外什么都不做。
因此,没有 "dynamic" 委托:只需实现一个同名的常规方法,然后将其 return 设为您需要的任何值。
def Mayfield
case some_attribute_value
when 'foo' then user.Mayfield
when 'bar' then something_else.Mayfield
end
end
如果我用delegate :Mayfield, to: :user
我可以在 :Mayfield 上调用什么来查看它是否已委托给 :user class?
解决方案: 不可能
正如@BKSpurgeon 已经指出的那样,delegate
方法的 implementation 除了创建一个在对象上调用给定方法的方法之外什么都不做。
因此,没有 "dynamic" 委托:只需实现一个同名的常规方法,然后将其 return 设为您需要的任何值。
def Mayfield
case some_attribute_value
when 'foo' then user.Mayfield
when 'bar' then something_else.Mayfield
end
end