Prevent the data from being updated if there is no stock of products

1

I work with two models, Buy and the other Bidon , from the Shopping form I am selecting the drums that I have available in the stock, let's say a quantity of 100 drums, after that, I enter a new quantity with the drums that I will need, say 110 drums, what happens is that when I overcome and I keep the amount the drums stock remains with number -10 for example, what I want to achieve is that when trying to save the new drum, I miss a message or something that tells me that I can not use more of the drums that exist and obviously avoid being stored with negative values.

class Bidon
  has_many :compras
end

class Compra
  after_save :descontar_bidones

  belongs_to :bidon

  private

  def descontar_bidones
    self.bidon.existencias -= self.cantidad_comprada
    self.bidon.save
  end
end

Since thank you very much and I hope you can guide me a bit.

    
asked by Mosiah Ricardo 04.01.2017 в 06:17
source

2 answers

1

What you need is to check if your existence is greater than your purchase before to call descontar_bidones , something like the following:

class Compra
  before_save :check_stock

  private   
  def check_stock
     unless self.bidon.existencias > self.cantidad_comprada
       errors[:attribute] << "Cantidad a comprar supera existencia"
       return false
     end
  end
end

Remember that before_save is executed after the validations ( validates ), in case you have any.

What this order of execution would leave you:

  • validates (if any)
  • before_save (that verifies if there is existence of drums)
  • after_save (which discounts the existence of drums)
  • answered by 04.01.2017 в 14:24
    0

    I wanted to adapt my code to yours only that I manage three models but I hope it works for you, I only add a rollback when the validation does not pass and send the error. In your controller you have to send to check_stock just after @compra = Compra.new (compra_params) I leave the code.

    Model

    class Compra
      belongs_to :bidon
    
       def check_stock
         ActiveRecord::Base.transaction do
          self.save
              self.bidon.existencias < self.cantidad_comprada
                    errors.add( "Cantidad a comprar supera existencia") 
                    raise ActiveRecord::Rollback 
                end
                self.bidon.existencias -= self.cantidad_comprada
                self.bidon.save
               end
            end
          end
        end
    

    Controller

    def create
        @compra = Compra.new(compra_params)
        respond_to do |format|
          if @compra.check_stock
            format.html { redirect_to @compra, notice: 'Compra was successfully created.' }
            format.json { render :show, status: :created, location: @compra }
          else
            format.html { render :new  }
            format.json { render json: @compra.errors, status: :unprocessable_entity }
          end
        end
      end
    
        
    answered by 27.07.2017 в 20:17