我正在实施一种优惠券/优惠券系统,用户可以为特定产品购买一对多的优惠券。例如,用户可以在零售商X上以折扣价购买两张用于购买软饮料的优惠券。优惠券可以在同一时间或在两个不同的时间点领取,即今天领取一张,明天领取另一张。
添加优惠券后,将生成订单以及每个优惠券和数量的相关order_item。
成功完成结帐后,我需要在quantity > 1
处复制优惠券,因为我需要为每个优惠券声明一个时间戳,以进行审核。
换句话说,我需要从以下位置更新OrderItem:
id,quantity,order_id,product_id,claimed_on
1, 2, 001, 010, nil
2, 3, 001, 020, nil
在购物车中至]
id,quantity,order_id,product_id,claimed_on 1, 1, 001, 010, nil 2, 1, 001, 020, nil 3, 1, 001, 010, nil 4, 1, 001, 020, nil 5, 1, 001, 020, nil
购买成功后。
到目前为止,我已经找到了这个answer,但我一直在努力从activerecord的角度看实现。我主要担心的是,当几个用户使用该平台时,会生成任何形式的锁定或损坏表。
我已经考虑过创建第三个表,该表将仅包含成功的订单,但似乎不是一个好习惯。
这是我的模特:
class Order < ApplicationRecord belongs_to :user has_many :order_items end
OrderItem看起来像:
# id :bigint(8) not null, primary key # quantity :integer default(0), not null # created_at :datetime not null # updated_at :datetime not null # order_id :bigint(8) # product_id :bigint(8) # # Indexes # # index_order_items_on_order_id (order_id) # index_order_items_on_product_id (product_id) # # Foreign Keys # # fk_rails_... (order_id => orders.id) # fk_rails_... (product_id => products.id) # class OrderItem < ApplicationRecord belongs_to :order belongs_to :product validates :quantity, numericality: { greater_than_or_equal_to: 0 } after_save :destroy_if_zero def total quantity * product.active_product_prices.price end private def destroy_if_zero destroy if quantity.zero? end end
更新:
我正在使用数据条来处理付款,因此Order模型有一个charge_id来存储数据条令牌-希望有帮助。
我正在实施一种优惠券/优惠券系统,用户可以为特定产品购买一对多的优惠券。例如,用户可以在零售商X上以折扣价购买两张抵用券。...
我不会将同一张表用于订购商品和优惠券/代金券。如果我在您的位置,则将删除order_item表中的Claim_on字段并创建一个子表