Posted 5 months ago. Visible to the public.

ActiveType #change_association: Define { autosave: true } on parent models

Consider the following models and form models:

class Parent < ApplicationRecord has_many :children, class_name: 'Child', foreign_key: 'parent_id' end class Parent::AsForm < ActiveType::Record[Parent] change_association :children, class_name: 'Child::AsForm', foreign_key: 'parent_id', autosave: true, inverse_of: :parent accepts_nested_attributes_for :children validates_associated :children end
class Child < ApplicationRecord belongs_to :parent, inverse_of: :children end class Child::AsForm < ActiveType::Record[Child] change_association :parent, class_name: 'Parent::AsForm', inverse_of: :children end

When saving a Parent::AsForm record with nested Child::AsForm records, the children will not be saved.
This behavior also applies to the case where you override the association with has_many :children, ... and belongs_to :parent, ....


This can be fixed with appending autosave: true to the Parent model:

class Parent < ApplicationRecord has_many :children, class_name: 'Child', foreign_key: 'parent_id', dependent: :destroy, autosave: true end

Then saving children of Parent::AsForm's will work as expected.

Your development team has a full backlog of feature requests, chores and refactoring coupled with deadlines? We are familiar with that. With our "DevOps as a Service" offering, we support developer teams with infrastructure and operations expertise.

Owner of this card:

Jakob Scholz
Last edit:
5 months ago
by Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Jakob Scholz to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more