Muhammad Yaseen Muhammad Yaseen - 5 months ago 11
Ruby Question

Delete the respective row completely if acutal quantity goes to zero

I want to delete the respective row completely if

tquantity = 0
.

Find the screenshot for better understanding as below;

screenshot.png

index.html.erb

<div class="col-md-10 col-md-offset-1">

<div class="table-responsive myTable">

<table class="table listing text-center">
<tr class="tr-head">
<td>DESCRIPTION</td>
<td>COLOR</td>
<td>QUANTITY</td>
<td>RETAIL PRICE</td>
<td>TOTAL AMOUNT</td>
<td>CARTON NO</td>
<td>CUSTOMER 1</td>
<td>CUSTOMER 2</td>
<td>ACTUAL QUANTITY</td>
</tr>

<% @purchases.each do |purchase| %>

<tr class="tr-<%= cycle('odd', 'even') %>">

<td class="col-2"><%= purchase.description %></td>
<td class="col-1"><%= purchase.color %></td>
<td class="col-2"><%= purchase.quantity %></td>
<td class="col-2"><%= number_with_precision(purchase.rprice, :delimiter => ",", :precision => 2) %></td>
<td class="col-2"><%= number_with_precision(purchase.tamount, :delimiter => ",", :precision => 2) %></td>
<td class="col-2"><%= purchase.cartonno %></td>
<td class="col-2"><%= purchase.cus1 %></td>
<td class="col-2"><%= purchase.cus2 %></td>
<td class="col-2"><%= tquantity = purchase.quantity - purchase.cus1 - purchase.cus2 %></td>

</tr>

<% end %>

</table>

</div>
</div>
</div>


purchases_controller.rb

class PurchasesController < ApplicationController
before_action :set_purchase, only: [:show, :edit, :update, :destroy]

# GET /Stockings
# GET /deldetails.json
def index
#@purchases = Purchase.all
@purchases = Purchase.where("tquantity !=?", 0)
end

def import
Purchase.import(params[:file])
redirect_to purchases_url, notice: "Purchases imported."
end

# GET /purchases/1
# GET /purchases/1.json
def show
end

# GET /purchases/new
def new
@purchase = Purchase.new
end

# GET /purchases/1/edit
def edit
end

# POST /purchases
# POST /purchases.json
def create
@purchase = Purchase.new(purchase_params)

respond_to do |format|
if @purchase.save
format.html { redirect_to @purchase, notice: 'Purchase was successfully created.' }
format.json { render :show, status: :created, location: @purchase }
else
format.html { render :new }
format.json { render json: @purchase.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /purchases/1
# PATCH/PUT /purchases/1.json
def update
respond_to do |format|
if @purchase.update(purchase_params)
format.html { redirect_to @purchase, notice: 'Purchase was successfully updated.' }
format.json { render :show, status: :ok, location: @purchase }
else
format.html { render :edit }
format.json { render json: @purchase.errors, status: :unprocessable_entity }
end
end
end

# DELETE /purchases/1
# DELETE /purchases/1.json
def destroy
@purchase.destroy
respond_to do |format|
format.html { redirect_to purchases_url, notice: 'Purchase was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_purchase
@purchase = Purchase.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def purchase_params
params.require(:purchase).permit(:season, :category, :articleno, :description, :color, :quantity, :rprice, :tamount, :cartonno, :cus1, :cus2, :tquantity )
end
end


purchase.rb

class Purchase < ActiveRecord::Base

def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
Purchase.create! row.to_hash
end
end
end


Do I need to use SQL WHERE clause, and if yes, then how would I obtain the desired result?

Answer

In your update action, you'll want to check to see if the new values cause the purchase to match the deletion criteria that you set. Use these changes to the update action to make that happen:

# PATCH/PUT /purchases/1
# PATCH/PUT /purchases/1.json
def update
  respond_to do |format|
    updated = @purchase.update(purchase_params)
    deleted = (@purchase.quantity - @purchase.cus1 - @purchase.cus2) <= 0
    if deleted
      @purchase.destroy
      format.html { redirect_to purchases_url, notice: 'Purchase was deleted.' }
      format.json { head :no_content }
    elsif updated
      format.html { redirect_to @purchase, notice: 'Purchase was successfully updated.' }
      format.json { render :show, status: :ok, location: @purchase }
    else
      format.html { render :edit }
      format.json { render json: @purchase.errors, status: :unprocessable_entity }
    end
  end
end

Regarding the view, in case there happen to be any rows that should be filtered in the data, you can use this query:

# GET /Stockings
# GET /deldetails.json
def index
  @purchases = Purchase.where("quantity - cus1 - cus2 > 0") # Only records with a valid total quantity
end

For the CSV import, the values can be filtered prior to creating the Purchase model object. Try changing to use this:

def self.import(file)
  CSV.foreach(file.path, headers: true) do |row|
    if (row["quantity"].to_i - row["cus1"].to_i - row["cus2"].to_i) > 0
      Purchase.create! row.to_hash
    end
  end
end

This will eliminate any invalid (zero quantity) Purchase model objects from being created in the application during the CSV import process. This assumes that the header fields are named "quantity", "cus1", and "cus2", and may need to be adjusted slightly to match the actual header field names.

Comments