

Rails 7 adds accepts_nested_attributes_for support for delegated_type
source link: https://blog.saeloun.com/2022/02/02/rails-7-adds-nested-attributes-for-delegated-type
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.

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
PR
to know more about this feature.
Before
For the sake of understanding,
let’s consider the following declaration of the Note
model.
# app/models/notes.rb
class Note < ApplicationRecord
delegated_type :authorable, types: %w[ Customer Employee ]
end
# app/models/employee.rb
class Employee < ApplicationRecord
end
Previously,
in order to create a new delegated_type
record,
we would do something like this:
emp_one = Employee.create!(name: "emp one")
Note.create!(authorable: emp_one, body: "sample text")
If we try to save the note using nested attributes, it will raise an error:
params = { note: { authorable_type: 'Employee', body: "sample text", authorable_attributes: { name: 'Emp one' } } }
Note.create!(params[:note])
# ArgumentError: Cannot build association `authorable'. Are you trying to build a polymorphic one-to-one association?
After
With Rails 7 adding support of accepts_nested_attributes_for
for delegated_type
we can save the records as follows:
# app/models/notes.rb
class Note < ApplicationRecord
delegated_type :authorable, types: %w[ Customer Employee ]
accepts_nested_attributes_for :authorable
end
params = { note: { authorable_type: 'Employee', body: "sample text", authorable_attributes: { name: 'Emp one' } } }
note = Note.create!(params[:note])
To know more about this feature, checkout this PR.
Recommend
-
14
Building a Rails API App Which Accepts Data from JavaScript May 10, 2017 A friend recently described a web tracking problem to me and we discussed looking at it via Google Analytics or by rolling our own appro...
-
27
Employee Experience
-
10
Rails: Nested resource scaffold Posted: 2007-01-23 - Last updated: 2019-06-05 Tagged
-
7
accepts_nested_attributes_for 的验证问题 出于性能的考虑,对商品表做了垂直分割,用两张表来保存商品的信息。 商品的概览(products) id title price 商品的图文介绍(products_details) id product_id d...
-
11
Rails 7 ActiveSupport::Cache::MemCacheStore now accepts an explicit nil for addresses argument May 11, 2021 , by Prateek Choudhary 1 minute...
-
3
Rails 7 adds attributes_for_database to return attributes as they would be in the database Sep 1, 2021 , by Swaathi Kakarla 1 minute read...
-
6
Copy link Contributor xtr3me commented...
-
3
Sunday, November 7, 2021 Nested attributes for delegated types, improved performances and more! Posted by robin850 Hey! Robin
-
8
Rails 7 now accepts "html" and "screenshot" kwargs for system test screenshot helper Aug 23, 2022 , by Swaathi Kakarla 2 min...
-
9
Repository files navigationState Machines State Machines adds support for creating state machines for attributes on any Ruby class. Please note that multiple integrations are available for
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK