Hi,
I made a patch today to my code to solve a use case and wonder whether or not it would be useful for ActiveRecord users in general.
My setup is
class Form
has_many :sections
has_many :questions, through: :sections
accepts_nested_attributes_for :sections
class
class Section
belongs_to :form
has_many :questions
accepts_nested_attributes_for :questions
class
class Question
belongs_to :section
end
My form lets the user edit sections and questions. Drag and drop is also allowed to change question order but also to change the section it belongs to.
With Rails code as is, it fails with a NotFound error, which is expected because each section looks for its own questions whenever a question has an id
attribute: https://github.com/rails/rails/blob/d9715fcde450cebec3aeff74d4d39db5542d6625/activerecord/lib/active_record/nested_attributes.rb#L453
I’d suggest something like:
class Section
belongs_to :form
has_many :questions
accepts_nested_attributes_for :questions, scope: ->{ form.questions }
class
to fetch existing records based on a custom scope.
Is this a desired feature? Should I submit a pull request?
Benjamin