mark_for_destruction在更新(嵌套对象)上不起作用Rails 5.1

时间:2018-07-19 00:19:57

标签: ruby-on-rails nested-forms

我有两个带有嵌套属性的模型:Order和Item。每个订单都有多个项目。

class Order < ApplicationRecord
  has_many :items, autosave: true
  accepts_nested_attributes_for :items,
    :allow_destroy => true
end

class Item < ApplicationRecord
  belongs_to :order
end    

我在Order模型上有一个setter方法,用于通过Orders视图和Controller处理诸如更新商品属性,创建新商品,删除商品之类的操作。

一切正常,但删除项目除外。

当用户输入:quantity为0时,我想删除一个项目。

这是Instance Setter方法的一部分,具有处理我要完成的逻辑(当用户输入0作为数量时,删除编辑订单视图上的项目(控制器上的更新订单)。

 elsif item[:id].present? && item[:qty].to_i <= 0
    order_item = self.items.find item[:id]
    order_item.mark_for_destruction

如果我打印order_item.marked_for_destruction?在elsif的末尾,我明白了!...问题是订单已成功更新,但商品仍保留先前的数量。

**更新**

这是Order模型上的整个setter方法。此实例方法将更新该订单上的商品(从“编辑订单”视图),销毁商品(当数量= 0时)和“创建商品”(从“新订单”视图)。

def items=(items=[])
items.select {|item| item[:id].present? || item[:qty].to_i.positive? }.each do |item|
  if item[:id].present? && item[:qty].to_i.positive?
    order_item = self.items.find item[:id]
    order_item.update quantity:  item[:qty],
                      unit:      item[:unit],
                      catch_weight_data: item[:catch_weight_data],
  elsif item[:id].present? && item[:qty].to_i <= 0
    order_item = self.items.find item[:id]
    order_item.mark_for_destruction
    puts "Was the item marked for destruction?"
    puts order_item.marked_for_destruction?
  else
    next unless item[:qty].to_i.positive?
    lp = LabeledProduct.find item[:labeled_product_id]
    order_item = self.items.new unit_price_in_cents:         lp.price_in_cents,
                                case_price_in_cents:         lp.case_price_in_cents,
                                data:                        lp.data,
                                quantity:                    item[:qty],
                                unit:                        item[:unit],
                                labeled_product_id:          item[:labeled_product_id],
                                customer_labeled_product_id: item[:customer_labeled_product_id],
                                taxable:                     item[:taxable],
                                item_weight:                 item[:item_weight]
   end
  end
 end

这是服务器日志...我打印了该项目是否标记为要销毁?和order_item.marked_for_destruction?紧跟在更新带有qty = 0 ...的项目的方法之后,如您所见,它显示为true:

Started PATCH "/admin/customers/customer/orders/113" for 127.0.0.1 at 
2018-07-19 10:26:53 -0700
Processing by Admin::Customer::OrdersController#update as HTML
Parameters: {"utf8"=>"✓","authenticity_token"=>"", "order"=> 
{"status"=>"Submitted", "picked_at"=>"", "fulfilled_at"=>"", "items_attributes"=>{"0"=>{"taxable"=>"1", "unit_price"=>"2.47", "id"=>"307"}}, "items"=>[{"id"=>"307", **"qty"=>"0"**, "unit"=>"UNIT", "catch_weight_data"=>["", ""]}...
 "commit"=>"Update Order", "customer_id"=>"customer", "id"=>"113"}
      User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
      Customer Load (0.3ms)  SELECT  "customers".* FROM "customers" WHERE "customers"."slug" = $1 LIMIT $2  [["slug", "customer"], ["LIMIT", 1]]
      Order Load (0.5ms)  SELECT  "orders".* FROM "orders" WHERE "orders"."customer_id" = $1 AND "orders"."id" = $2 ORDER BY "orders"."created_at" DESC LIMIT $3  [["customer_id", 1], ["id", 113], ["LIMIT", 1]]
       (0.2ms)  BEGIN
      Item Load (0.4ms)  SELECT  "items".* FROM "items" WHERE "items"."order_id" = $1 AND "items"."id" = $2 LIMIT $3  [["order_id", 113], ["id", 307], ["LIMIT", 1]]
      Warehouse Load (0.2ms)  SELECT  "warehouses".* FROM "warehouses" 
      INNER JOIN "labeled_products" ON "warehouses"."id" = 
      "labeled_products"."warehouse_id" WHERE "labeled_products"."id" 
      = $1 LIMIT $2  [["id", 22215], ["LIMIT", 1]]


    Was the item marked for destruction?
    true

    Item Load (0.4ms)  SELECT "items".* FROM "items" WHERE 
    "items"."order_id" = $1 AND "items"."id" = 307  [["order_id",113]]
    (0.2ms)  COMMIT
    Redirected to 
    http://localhost:3000/admin/customers/customer/orders/113
    Completed 302 Found in 30ms (ActiveRecord: 2.4ms)

控制器操作:

def update
respond_to do |format|
  if @order.update(order_params)
    format.html { redirect_to [:admin, @customer, @order], notice: "Order was successfully updated." }
    format.json { render :show, status: :ok, location: [:admin, @customer, @order] }
  else
    format.html { render :edit }
    format.json { render json: @order.errors, status: :unprocessable_entity }
  end
end

结束

一些指导将不胜感激。

谢谢!

0 个答案:

没有答案