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

Re-creating deleted stock items #3834

Closed
steveh opened this issue Oct 7, 2013 · 1 comment
Closed

Re-creating deleted stock items #3834

steveh opened this issue Oct 7, 2013 · 1 comment

Comments

@steveh
Copy link

steveh commented Oct 7, 2013

Disclaimer: I'm not sure if this is a bug or I've misunderstood how this should work.

If I have a StockItem for a Variant and StockLocation, and I delete that StockItem, it remains in the database, due to acts_as_paranoid.

However, I can't re-create it, due to a uniqueness constraint:
validates_uniqueness_of :variant_id, scope: :stock_location_id.

I see TaxCategory adds deleted_at to the scope, i.e.
validates_uniqueness_of :variant_id, scope: [:stock_location_id, :deleted_at] ?

But I'm not sure that's right either, as undeleted stock items will have a NULL deleted_at, and NULL != NULL in SQL, so that uniqueness constraint will allow multiple undeleted products.

Steps to reproduce:

  1. Add some stock to a variant in a given location
  2. Delete that stock
  3. Try and add some to stock to the same variant and the same location
  4. ActiveRecord::RecordInvalid (Validation failed: Variant has already been taken)

Expected behaviour:

  • Able to add new stock without regard to stock that has been previously deleted

Versions:

  • Spree 2.1.1
  • Rails 4.0.0

Gemfile and stack trace: https://gist.github.com/steveh/6862586

I've added this (horrible) workaround for the time being:

module Spree
  StockItem.class_eval do
    _validators.reject!{ |key, _| key == :variant_id }

    _validate_callbacks.reject! do |callback|
      callback.raw_filter.try(:attributes) == [:variant_id]
    end

    validates_uniqueness_of :variant_id, scope: [:stock_location_id, :deleted_at]
  end
end
@huoxito huoxito closed this as completed in 0d76a21 Oct 7, 2013
huoxito added a commit that referenced this issue Oct 7, 2013
Add deleted_at to uniqueness scope otherwise use cant recreate a stock
item on the same stock location

Fixes #3834
@huoxito
Copy link
Member

huoxito commented Oct 7, 2013

Hi @steveh it seems to me your suggestion works just fine. Could you please update and confirm thats fixed? thanks for reporting.

huoxito added a commit that referenced this issue Oct 7, 2013
Add deleted_at to uniqueness scope otherwise use cant recreate a stock
item on the same stock location

Fixes #3834
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