我有一组对象,其中一些具有organization_id属性,另一些则委托(delegate)organization_id。
例如:
class Department
belongs_to :organization
end
class Program
belongs_to :department
delegate :organization_id, to: :department
end
class Role
belongs_to :organization
end
class UsersRole
belongs_to :role
delegate :organization_id, to: :role
end
我想要一种方法来以编程方式确定organization_id被委托(delegate)给哪个对象类,以便我可以执行如下操作
def find_organization_id_source(object)
object.organization_id.method_im_hoping_exists
end
$ find_organization_id_source(@program)
>> Department
$ find_organization_id_source(@users_role)
>> Role
最佳答案
这是不可能的。从active_support/core_ext/module/delegation.rb
的来源可以看出,Rails 创建一个新方法,该方法将在内部调用委托(delegate)方法。
关于ruby-on-rails - 是否可以以编程方式确定在 ruby 中将方法委托(delegate)给哪个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35047529/