

Add accepts_nested_attributes_for support for delegated_type by xtr3me · Pull Re...
source link: https://github.com/rails/rails/pull/41717
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Summary
This PR adds nested_attributes_for
support to delegated_type
, this allows a developer to create and update records easily without needing to write specific methods like:
class Entry < ApplicationRecord delegated_type :entryable, types: %w[ Message Comment ] def self.create_with_comment(content, creator: Current.user) create! entryable: Comment.new(content: content), creator: creator end end
Using nested_attributes_for
allows to execute the following:
class Entry < ApplicationRecord delegated_type :entryable, types: %w[ Message Comment ] accepts_nested_attributes_for :entryable end params = { entry: { entryable_type: 'Comment', entryable_attributes: { content: 'Smiling' } } } entry = Entry.create(params[:entry])
Nested forms based on accepts_nested_attributes_for
are very powerful and this is the last piece missing to also be able to use it on Delegated Types.
Question
Since this is a polymorphic belongs_to
relationship, what other tests would we like to have? As it is already tested in TestNestedAttributesOnABelongsToAssociation
Recommend
-
86
README.md Delegated
-
92
The Observable (not to be confused with the Rx Observable) is a pretty neat language feature, making you able to write listeners in a few lines of code.
-
42
We run one of the largest Transport Layer Security (TLS) deployments in the world, and we are continually looking at ways to move TLS security forward. Last year, we shared how we led the deployment of
-
27
Employee Experience
-
7
accepts_nested_attributes_for 的验证问题 出于性能的考虑,对商品表做了垂直分割,用两张表来保存商品的信息。 商品的概览(products) id title price 商品的图文介绍(products_details) id product_id d...
-
6
Conversation Copy link ...
-
3
Sunday, November 7, 2021 Nested attributes for delegated types, improved performances and more! Posted by robin850 Hey! Robin
-
6
Rails 6.1 added delegated_type which resembles polymorphic relations. Using delegated_type the developer is introduced with a bunch of useful methods and scopes. Checkout this P...
-
4
Nested type-safe DI Containers by Mark Seemann How to address the arity problem with type-safe DI Container prototypes. This article is part of a series called
-
7
Deserialize a Serialized Nested Type Within a JSON ObjectDownload
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK