---
title: "Rails 7 adds accepts_nested_attributes_for support for delegated_type"
description:
  "Rails 7 adds accepts_nested_attributes_for support for delegated_type"
canonical_url: "https://www.bigbinary.com/blog/rails-7-adds-accepts-nested-attributes-for-support-for-delegated_type"
markdown_url: "https://www.bigbinary.com/blog/rails-7-adds-accepts-nested-attributes-for-support-for-delegated_type.md"
---

# Rails 7 adds accepts_nested_attributes_for support for delegated_type

Rails 7 adds accepts_nested_attributes_for support for delegated_type

- Author: Ashik Salman
- Published: November 23, 2021
- Categories: Rails, Rails 7

Rails 6.1 introduced the `delegated_type` to Active Record, which makes it
easier for models to share responsibilities. Please see our
[blog](https://www.bigbinary.com/blog/rails-6-1-adds-delegated-type-to-active-record)
to read more about `delegated_type`.

```ruby
class Entry < ApplicationRecord
  # Schema
  #  entryable_type, entryable_id, ...
  delegated_type :entryable, types: %w[ Message Comment ]
end

class Message
  # Schema
  #  subject, ...
end

class Comment
  # Schema
    #  content, ...
end
```

The `accepts_nested_attributes_for` option is very helpful while handling nested
forms. We can easily create and update associated records by passing details
along with the main object parameters when the `accepts_nested_attributes_for`
option is enabled.

### Before

The `accepts_nested_attributes_for` option is not available for
`delegated_type`, hence we can't use nested forms for associated objects
configured via `delegated_type`.

### Rails 7 onwards

Rails 7 adds `accepts_nested_attributes_for` support to `delegated_type`, which
allows to create and update records easily without needing to write specific
methods or logic.

```ruby
class Entry < ApplicationRecord
  delegated_type :entryable, types: %w[ Message Comment ]
  accepts_nested_attributes_for :entryable
end

params = {
  entry: {
    entryable_type: 'Message',
    entryable_attributes: { subject: 'Delegated Type' }
  }
}

message_entry = Entry.create(params[:entry])

params = {
  entry: {
    entryable_type: 'Comment',
    entryable_attributes: { content: 'Looks Cool!' }
  }
}

comment_entry = Entry.create(params[:entry])
```

If we want to deal with more logic or validations while creating the entryable
objects, we'll have to create a method specifically and do the logic there.

```ruby
class Entry < ApplicationRecord
  def self.create_with_comment(content)
    # Validation logic goes here
    create! entryable: Comment.new(content: content)
  end
end
```

Please check out this [pull request](https://github.com/rails/rails/pull/41717)
for more details.

## Links

- [Human page](https://www.bigbinary.com/blog/rails-7-adds-accepts-nested-attributes-for-support-for-delegated_type)
