ruby ActiveRecord::Base with SimpleDelegator抛出警告

pieyvz9o  于 2023-04-29  发布在  Ruby
关注(0)|答案(1)|浏览(101)

如何删除警告warning: delegator does not forward private method #to_ary
以下是使用给定技术重新创建问题的代码片段:

  • Ruby 3.2.1
  • Rails www.example. com
class Foo < ActiveRecord::Base
  def name
    "foobar"
  end
end

class FooDelegator < SimpleDelegator
  def name
    "name"
  end
end

pry(main)> [FooDelegator.new(Foo.new)].flatten
(pry):12: warning: delegator does not forward private method #to_ary
=> [#<Foo:0x00007f3fa3b1ebf0 id: nil, created_at: nil, updated_at: nil>]

编辑:这个问题也发布在Github上,因为我认为它实际上属于ActiveRecord(rails)。
链接https://github.com/rails/rails/issues/48059

ozxc1zmp

ozxc1zmp1#

在ActiveRecord类中使用public :to_ary将private方法转换为public方法:

class Foo < ActiveRecord::Base
  def name
    "foobar"
  end

  public :to_ary
end

class FooDelegator < SimpleDelegator
  def name
    "name"
  end
end

相关问题