Deleted products crash the wishlist view
brchristian opened this issue · comments
My app uses wished_products.count
to tell the user in the head of the page how many items are in their Wish List.
After upgrading Spree to 2-0-master
, I saw "Wish List (1)" but upon clicking, my application crashed, citing:
undefined method `product' for nil:NilClass
at the line product = variant.product.
As you can see at the source, the default scoping of the Variant
model (which itself comes from acts_as_paranoid
) will create these types of nil
variants, with the potential to crash spree_wishlist
.
I'm not sure what the solution is in this case. Namely, I'm not sure whether we should have a way to purge wishlist items that refer to deleted objects as we find them, or whether we should have a subtler way of determining the logical size of the wishlist (scoped to not deleted products/variants), or what exactly is the conceptual approach to this.
I'd be happy to suggest a PR if someone from the spree team (@radar?) wants to comment on how best to address it.
Personally I think the cleanest would be to create a railties migration that deletes at migration time any WishedProduct
s with nil
Variant
s, and then to prevent future problems by using an after_destroy
hook to the Variant
model.
Yes, you're on the right track with the migration and the after_destroy hook.
I think we should just have a class decorator for the Variant
model which defines this:
Spree::Variant.class_eval do
has_many :wished_products, :dependent => :destroy
end
Ok, great :)