brendon / acts_as_list

An ActiveRecord plugin for managing lists.
http://brendon.github.io/acts_as_list/
MIT License
2.04k stars 355 forks source link

touch parent broken on destroy #397

Closed ConfusedVorlon closed 2 years ago

ConfusedVorlon commented 2 years ago
class Foo < ApplicationRecord
  acts_as_list
  belongs_to :bar, touch: true
end

class Bar < ApplicationRecord
  has_many :foos, dependent: :destroy
end

RSpec.describe Foo, type: :model do
    it "touches parent on destroy" do
        bar = create(:bar)
        foo = create(:foo, bar: bar)

        expect {
            foo.destroy
        }.to change(bar,:updated_at)
    end
end

the test fails if Foo is acts_as_list if you remove acts_as_list from Foo, then the test passes (as expected)

Thanks for a great gem :)

ConfusedVorlon commented 2 years ago

never mind - rewriting the test as

    expect {
        foo.destroy
    }.to change{bar.reload.updated_at}

makes it pass, so I guess the data is actually getting updated as required. Just a testing quirk...

brendon commented 2 years ago

No worries :) Glad you worked it out :) You'd definitely need to reload the record to see the change.

ConfusedVorlon commented 2 years ago

wierdly you don't need to reload if you're not using acts_as_list. that's what threw me; I tested a non-list-model like this first and it passed...

I guess something about the work acts_as_list is doing in the destroy callback...

brendon commented 2 years ago

Yea that's strange. Not sure what could be causing it :) Feel free to investigate it further if you think it's worth your time :D