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

Unable to delete a parent with dependent destroy #37

Open
onerinas opened this issue Mar 5, 2025 · 5 comments
Open

Unable to delete a parent with dependent destroy #37

onerinas opened this issue Mar 5, 2025 · 5 comments

Comments

@onerinas
Copy link

onerinas commented Mar 5, 2025

I'm seeing below error when deleting a parent model which has dependent destroy

undefined method `+' for nil ["/Users/rinas/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/positioning-0.2.6/lib/positioning/mechanisms.rb:43:in `destroy_position'",

e.g,

class TodoList
  has_many :todos, dependent: :destroy
end


class Todo
  # has_ancestry cache_depth: true, counter_cache: true, primary_key_format: "[-A-Fa-f0-9]{36}"

  belongs_to :todo_list
  belongs_to :todo_list_version

  # positioned on: [:todo_list, :todo_list_version, :ancestry]
  positioned on: [:todo_list]
end

Error occurs when I do todo_list.destroy which has multiple todos and has position. I have commented out few lines to add some context. I don't think that's the reason but still keeping it in case it's helpful.

I'm on on 0.2.6. Any other details I can add that might help find what's happening?

@onerinas
Copy link
Author

onerinas commented Mar 5, 2025

Tested in latest 0.4.5:

undefined method `+' for nil ["/Users/rinas/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/positioning-0.4.5/lib/positioning/mechanisms.rb:49:in `destroy_position'",

@onerinas
Copy link
Author

onerinas commented Mar 5, 2025

@brendon Is it because of multiple scopes?

Looking at

@positioned.destroyed_by_association.foreign_key == scope_column
we are comparing
@positioned.destroyed_by_association.foreign_key == scope_column

Could that be the reason?

@brendon
Copy link
Owner

brendon commented Mar 5, 2025

Hi @onerinas, that's an interesting one. It appears to be failing to detect that it's being deleted via a scope.

It shouldn't matter but try not passing in an array:

positioned on: :todo_list

Here's the current test for this scenario:

def test_destroyed_via_positioning_scope?
list = List.create name: "List"
student = list.authors.create name: "Student", type: "Author::Student"
teacher = list.authors.create name: "Teacher", type: "Author::Teacher"
mechanisms = Positioning::Mechanisms.new(student, :position)
refute mechanisms.send(:destroyed_via_positioning_scope?)
mechanisms = Positioning::Mechanisms.new(teacher, :position)
teacher.destroy
refute mechanisms.send(:destroyed_via_positioning_scope?)
list.destroy
assert mechanisms.send(:destroyed_via_positioning_scope?)
end

If you feel up to it, can you try to recreate the issue with a test and then create a PR with that failing test and I'll see what I can find.

What version or Rails are you on?

@onerinas
Copy link
Author

onerinas commented Mar 6, 2025

@brendon In my actual code, I have more than one column used and hence the array like this:

positioned on: [:todo_list, :todo_list_version, :ancestry]

I’ll try to add a test to see if it fails. I hope that would be helpful.

@brendon
Copy link
Owner

brendon commented Mar 6, 2025

It’ll definitely be helpful. Is your parent model double linked to the child with two dependent: destroys?

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