Reputation: 6694
I have an after_destroy
callback that I'd expect to return nil
but instead still has a value.
class WeighIn < ActiveRecord::Base
belongs_to :check_in
after_destroy :add_employee_weightloss
def add_employee_weightloss
p self.check_in.weigh_in.present? # returns true
end
end
specs:
it "employee weightloss" do
ci = CheckIn.create()
wi = WeighIn.create(check_in_id: ci.id)
wi.destroy
expect(wi.reload).to eq(nil) # returns wi instead of nil
end
Upvotes: 0
Views: 1280
Reputation: 44611
You should use destroyed?
(or exists?
, or persisted?
) instead, cause present?
just checks if the object is present, which is the correct behaviour after destruction (destroy
itself returns deleted object).
def add_employee_weightloss
p check_in.weigh_in.destroyed?
end
Also you should not use the following:
expect(wi.reload).to eq(nil)
cause if wi
was destroyed you are going to get ActiveRecord::RecordNotFound
exception instead of nil
. You can try the following:
it "employee weightloss" do
wi = WeighIn.create(check_in: CheckIn.create)
wi.destroy
expect(wi.destroyed?).to eq(true)
end
Upvotes: 1