如果实际数量变为零,则完全删除相应行
Delete the respective row completely if acutal quantity goes to zero
如果 tquantity = 0
我想完全删除相应的行。
找到下面的屏幕截图以便更好地理解;
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
我是否需要使用 SQL WHERE 子句,如果需要,我将如何获得所需的结果?
我遇到了类似的问题并使用 destroy_all
而不是 destroy
解决了这个问题。这清除了为我显示的行。让我知道是否有帮助。
在您的 update
操作中,您需要检查新值是否会导致购买符合您设置的删除条件。使用对 update
操作的这些更改来实现这一点:
# 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
关于视图,如果数据中恰好有需要过滤的行,可以使用这个查询:
# GET /Stockings
# GET /deldetails.json
def index
@purchases = Purchase.where("quantity - cus1 - cus2 > 0") # Only records with a valid total quantity
end
对于 CSV 导入,可以在创建购买模型之前过滤值 object。尝试更改为使用此:
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
这将消除在 CSV 导入过程中在应用程序中创建的任何无效(零数量)采购模型 object。这假设 header 字段被命名为 "quantity"、"cus1" 和 "cus2",并且可能需要稍微调整以匹配实际的 header 字段名称。
如果 tquantity = 0
我想完全删除相应的行。
找到下面的屏幕截图以便更好地理解;
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
我是否需要使用 SQL WHERE 子句,如果需要,我将如何获得所需的结果?
我遇到了类似的问题并使用 destroy_all
而不是 destroy
解决了这个问题。这清除了为我显示的行。让我知道是否有帮助。
在您的 update
操作中,您需要检查新值是否会导致购买符合您设置的删除条件。使用对 update
操作的这些更改来实现这一点:
# 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
关于视图,如果数据中恰好有需要过滤的行,可以使用这个查询:
# GET /Stockings
# GET /deldetails.json
def index
@purchases = Purchase.where("quantity - cus1 - cus2 > 0") # Only records with a valid total quantity
end
对于 CSV 导入,可以在创建购买模型之前过滤值 object。尝试更改为使用此:
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
这将消除在 CSV 导入过程中在应用程序中创建的任何无效(零数量)采购模型 object。这假设 header 字段被命名为 "quantity"、"cus1" 和 "cus2",并且可能需要稍微调整以匹配实际的 header 字段名称。