Search code examples
ruby-on-railsrubyactiverecord

ActiveRecord::Base with SimpleDelegator throws Warning


How do I remove the warning warning: delegator does not forward private method #to_ary?

Following is a code snippet for recreating the problem with the given technologies:

  • Ruby 3.2.1
  • Rails 7.0.4.3
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>]

EDIT: This issue was also posted on Github because I figured that it actually belongs to ActiveRecord (rails).

Link: https://github.com/rails/rails/issues/48059


Solution

  • Use public :to_ary in the ActiveRecord class to convert the private method to public:

    class Foo < ActiveRecord::Base
      def name
        "foobar"
      end
    
      public :to_ary
    end
    
    class FooDelegator < SimpleDelegator
      def name
        "name"
      end
    end