---
title: "Rails 5.1 adds delegate_missing_to"
description:
  "Rails 5.1 has introduced `Module#delegate_missing_to` to keep decorators more
  clearer"
canonical_url: "https://www.bigbinary.com/blog/rails-5-1-adds-delegate-missing-to"
markdown_url: "https://www.bigbinary.com/blog/rails-5-1-adds-delegate-missing-to.md"
---

# Rails 5.1 adds delegate_missing_to

Rails 5.1 has introduced `Module#delegate_missing_to` to keep decorators more
clearer

- Author: Neeraj Singh
- Published: May 30, 2017
- Categories: Rails 5.1, Rails

When we use `method_missing` then we should also use
[respond_to_missing?](http://blog.marc-andre.ca/2010/11/15/methodmissing-politely/).
Because of this code becomes verbose since both `method_missing` and
`respond_to_missing?` need to move in tandem.

DHH in [the issue](https://github.com/rails/rails/issues/23824) itself provided
a good example of this verbosity.

```ruby

class Partition
  def initialize(first_event)
    @events = [ first_event ]
  end

  def people
    if @events.first.detail.people.any?
      @events.collect { |e| Array(e.detail.people) }.flatten.uniq
    else
      @events.collect(&:creator).uniq
    end
  end

  private
    def respond_to_missing?(name, include_private = false)
      @events.respond_to?(name, include_private)
    end

    def method_missing(method, *args, &block)
      @events.public_send(method, *args, &block)
    end
end
```

He proposed to use a new method `delegate_missing_to`. Here is how it can be
used.

```ruby
class Partition
  delegate_missing_to :@events

  def initialize(first_event)
    @events = [ first_event ]
  end

  def people
    if @events.first.detail.people.any?
      @events.collect { |e| Array(e.detail.people) }.flatten.uniq
    else
      @events.collect(&:creator).uniq
    end
  end
end
```

## Why not SimpleDelegator

We at BigBinary have used
[SimpleDelegator](https://ruby-doc.org/stdlib-2.2.1/libdoc/delegate/rdoc/SimpleDelegator.html).
However one issue with this is that statically we do not know to what object the
calls are getting delegated to since at run time the delegator could be
anything.

DHH had following to
[say](https://github.com/rails/rails/issues/23824#issuecomment-221171899) about
this pattern.

> I prefer not having to hijack the inheritance tree for such a simple feature.

## Why not delegate method

[Delegate](https://apidock.com/rails/Module/delegate) method works. However here
we need to white list all the methods and in some cases the list can get really
long. Following is a real example from a real project.

```ruby
delegate :browser_status, :browser_stats_present?,
         :browser_failed_count, :browser_passed_count,
         :sequential_id, :project, :initiation_info,
         :test_run, success?,
         to: :test_run_browser_stats
```

## Delegate everything

Sometimes we just want to delegate all missing methods. In such cases method
`delegate_missing_to` does the job neatly. Note that the delegation happens to
only `public` methods of the object being delegated to.

Check out the [pull request](https://github.com/rails/rails/pull/23930) for more
details on this.

## Links

- [Human page](https://www.bigbinary.com/blog/rails-5-1-adds-delegate-missing-to)
