acts_as_list icon indicating copy to clipboard operation
acts_as_list copied to clipboard

How does one update position on a self referential model?

Open staycreativedesign opened this issue 1 year ago • 1 comments

class Section < ApplicationRecord

  acts_as_list scope: %i[course_id, master_section]
  acts_as_list top_of_list: 1
  scope :ordered, -> { order(:position) }

  belongs_to :course
  has_many :section_classes, class_name: 'Section', foreign_key: 'section_class_id'
  belongs_to :master_section, class_name: 'Section', foreign_key: 'section_class_id', optional: true
 
end

As you can see I have sections that sometimes have section_classes , since each section_class is a section how do I prevent from sections_clases updating the parent section it is from and vice verse?

How would I distinguish between a parent and a child?

staycreativedesign avatar Jan 05 '24 18:01 staycreativedesign

Hi @staycreativedesign, I don't think acts_as_list supports being invoked more than one time on a particular model, though I could be wrong.

The first invocation you have in your class should be enough to allow you to maintain lists scoped by a parent (as long as master_section_id is null at the top of the tree). I do something similar with the ancestry gem to have an ordered tree structure.

The top of list is 1 by default too I think (again, haven't checked the code) so you don't need to declare that.

brendon avatar Jan 05 '24 20:01 brendon

@staycreativedesign, did that answer your question? If so, feel free to close the issue or provide some more information :)

brendon avatar May 31 '24 08:05 brendon

Closing this due to lack of contact. Check out https://github.com/brendon/positioning which supports multiple lists per model.

brendon avatar Jun 04 '24 02:06 brendon