brendon / acts_as_list

An ActiveRecord plugin for managing lists.

Home Page:http://brendon.github.io/acts_as_list/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How does one update position on a self referential model?

staycreativedesign opened this issue · 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?

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.

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

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