Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How does one update position on a self referential model? #426

Open
staycreativedesign opened this issue Jan 5, 2024 · 1 comment
Open

Comments

@staycreativedesign
Copy link

staycreativedesign commented Jan 5, 2024

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?

@brendon
Copy link
Owner

brendon commented Jan 5, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants