为每个新实例实例化(刷新)的类变量

我有两个类别类别 order_item ,其中order_item 属于一个类别。

我想在每次检测到新类别时将唯一的“类别”项添加到作为“类别”数组并由数组类别变量 @@ categoriesList 表示的类别变量中在实例变量之间。

这是我尝试过的。

class OrderItemsController < ApplicationController

@@categoriesList = []

def create
    @order_item = OrderItem.new(order_item_params)
    if @order_item.save

      @total = @order_item.order.total
      @orderItemCategory = @order_item.category

      if @orderItemCategory.set?
        if !(@@categoriesList.include? @orderItemCategory)
            @total += @orderItemCategory.price
            @@categoriesList.push(@orderItemCategory)
        end
........
........
end

代码说明:

如果已考虑到属于同一类别价格的上一个order_item的价格,则我不希望考虑下一个 order_item 实例的价格。

例如:鸡蛋和牛奶都属于Combo-1。所以我只想考虑一次Combo-1的价格,而不是每个order_item实例(即鸡蛋和牛奶)的价格,这会使总金额增加一倍。

我尝试过的事情:

在考虑了价格之后,我按了order_item的类别名称。并且在创建下一个order_item时,我通过在当前 @@ categoriesList 类变量中检查该order_item类别的价格,来检查该价格是否已经记录。

问题: 在每个实例中,当我检查类变量 @@ categoriesList 时,它都会返回一个空数组列表,并且不会显示以前已推送到该数组的记录。

我想要类似Java中的静态变量的类,其中该类的每个实例共享相同的变量,而无需实际刷新每个实例的变量中的数据。

braveboy0 回答:为每个新实例实例化(刷新)的类变量

您几乎不需要类变量,因为它们不是线程安全的,也不是跨请求的持久变量,而这实际上适用于几乎所有编程语言。每次重新加载代码时,都会重置类变量。

您可以通过正确建模域来真正完成您的尝试。对于结帐系统,它已经完成了十亿次,并且常见的模式是:

class Order < ApplicationRecord
  has_many :line_items
  has_many :products,through: :line_items
end

# rails g model line_item quantity:decimal unit_price:decimal order:belongs_to product:belongs_to
class LineItem < ApplicationRecord
  belongs_to :order
  belongs_to :product
end

class Product < ApplicationRecord
  has_many :line_items
  has_many :orders,through: :line_items
end

在表示订单上实际行的行项目表中,您存储该项目所属的订单,购买时的数量和单价。要对订单进行总计,请对订单项求和:

class LineItem < ApplicationRecord
  # ...
  def net
    quantity * unit_price
  end
end
class Order < ApplicationRecord
   # ...
  def net
    line_items.sum(&:net)
  end
end

因此,您只需致电order.net,它就会为您带来净额。我不知道您将如何处理这些类别的混乱情况,但是如果我们在此处通过查找产品来查询价格,除非价格完全静态,否则我们将无法考虑过去的交易。

这是处理创建订单项的方法:

resources :orders do
  resources :line_items
end
class LineItemsController < ApplicationController
  before_action :set_order

  # GET /orders/1/line_items
  def new
    @line_item = @order.line_items.new
    @products = Product.all
  end

  # POST /orders/1/line_items
  def create
    @line_item = @order.line_items.new(line_item_params) do |items|
      items.unit_price = li.product.price # stores the price at time of purchase
    end
    if @line_item.save
      redirect_to @order
    else
      @products = Product.all
      render :new
    end
  end

  # ...

  private

  def line_item_params
    params.require(:line_item)
          .permit(:quantity,:product_id)
  end

  def set_order
    @order = Order.find(params[:order_id])
  end
end
<%= form_with(model: [@order,@line_item],local: true) do |f| %>
  <div class="field">
    <%= f.label :product_id %>
    <%= f.collection_select :product_id,@products,:id,:name %>
  </div>
  <div class="field">
    <%= f.label :quantity %>
    <%= f.number_field :quantity %>
  </div>

  <%= f.submit %>
<% end %>
本文链接:https://www.f2er.com/2444214.html

大家都在问