2016-07-01 40 views
0

我正在用rails製作商店。一個商店有訂單,order_items,用戶..Order_items在order_cleaner.rb中設置背景檢查後不會過期

現在,當顧客將order_item添加到購物車時,自動創建具有唯一ID的訂單,並將order_items保存在order_items模型下。還有一個驗證說,一個訂單隻允許3個order_items。

如果可能,我想刪除5分鐘後保存到模型中的那些order_items。保存所有3個order_items後5分鐘計數器應該啓動。

我做了什麼:

我添加了order_cleaner。到配置/初始化/ order_cleaner.rb爲了做後臺進程,並定期檢查數據庫,並刪除這些保存ORDER_ITEMS ..

這裏是如何看起來:

Thread.new do 
    while true 
    # Get all orders with at least 3 order_items 
    orders = Orders.joins(:order_items).group('orders.id').having('count(order_id) >= 3') 
    orders.each do |o| 
     # Delete associated order_item if it's older than 5 minutes 
     o.order_items.each {|oi| oi.destroy! if oi.updated_at < 5.minutes.ago } 
    end 
    sleep 1.minute 
    end 
end 

我回來檢查後5分鐘後添加3個order_items並且所有order_items仍然存在。他們還沒有被刪除。有什麼可能是這個問題,你有任何其他解決方案來實現這一目標?

感謝

相關代碼:

class Order < ActiveRecord::Base 
    belongs_to :order_status 
    belongs_to :user 
    has_many :order_items 
    validates_length_of :order_items, maximum: 3 #only 3 order_items are allowed within an order for each user 
    before_create :set_order_status 
    before_save :update_subtotal 



    def subtotal 
    order_items.collect { |oi| oi.valid? ? (oi.quantity * oi.unit_price) : 0 }.sum 
    end 
private 
    def set_order_status 
    self.order_status_id = 1 
    end 

    def update_subtotal 
    self[:subtotal] = subtotal 
    end 



end 

order_item.rb

class OrderItem < ActiveRecord::Base 
    belongs_to :product 
    belongs_to :order 
    validates_associated :order 
    validates :quantity, presence: true, numericality: { only_integer: true, greater_than: 0 } 
    validate :product_present 
    validate :order_present 


    before_save :finalize 

    def unit_price 
    if persisted? 
     self[:unit_price] 
    else 
     product.price 
    end 
    end 

    def total_price 
    unit_price * quantity 
    end 

private 
    def product_present 
    if product.nil? 
     errors.add(:product, "is not valid or is not active.") 
    end 
    end 

    def order_present 
    if order.nil? 
     errors.add(:order, "is not a valid order.") 
    end 
    end 

    def finalize 
    self[:unit_price] = unit_price 
    self[:total_price] = quantity * self[:unit_price] 
    end 


end 

order_items_controller.rb

class OrderItemsController < ApplicationController 
def create 
    @order = current_order 
    @order_item = @order.order_items.new(order_item_params) 
    @order.user_id = current_user.id 
    @order.save 
    session[:order_id] = @order.id 


    respond_to do |format| 
    format.js { flash[:notice] = "ORDER HAS BEEN CREATED." } 
    end 
end 



    def update 
    @order = current_order 
    @order_item = @order.order_items.find(params[:id]) 
    @order_item.update_attributes(order_item_params) 
    @order_items = @order.order_items 
    end 



    def destroy 
    @order = current_order 
    @order_item = @order.order_items.find(params[:id]) 
    @order_item.destroy 
    @order_items = @order.order_items 
    end 


private 

    def order_item_params 
    params.require(:order_item).permit(:quantity, :product_id, :user_id) 

end 
end 

schema.rb

create_table "order_items", force: :cascade do |t| 
    t.integer "product_id" 
    t.integer "order_id" 
    t.decimal "unit_price", precision: 12, scale: 3 
    t.integer "quantity" 
    t.decimal "total_price", precision: 12, scale: 3 
    t.datetime "created_at",       null: false 
    t.datetime "updated_at",       null: false 
    t.boolean "has_ordered" 
    end 

create_table "orders", force: :cascade do |t| 
    t.decimal "subtotal",    precision: 12, scale: 3 
    t.decimal "tax",     precision: 12, scale: 3 
    t.decimal "shipping",    precision: 12, scale: 3 
    t.decimal "total",    precision: 12, scale: 3 
    t.integer "order_status_id" 
    t.datetime "created_at",         null: false 
    t.datetime "updated_at",         null: false 
    t.integer "user_id" 
    t.boolean "ordered" 
    t.date  "first_item_added_at" 
    t.date  "first_order_added_at" 
    end 

回答

1

有在Orders.joins的錯誤應該是Order.joins

,你也可以使用寶石一樣https://github.com/plashchynski/crono安排作業,在cron用於UNIX的方式。

+0

你是什麼意思「安排部分工作」?我沒有設置任何其他的東西...... –

+0

對不起,我沒有閱讀初始化程序代碼中的'while',我認爲在代碼運行時數據庫連接沒有完全準備好。你應該嘗試把這個移動到一個像sidekiq這樣的跑步者的ActiveJob,並使用類似crono的東西來安排這個工作。 – neydroid

+0

你可以使用'Order.connected?'來檢查連接是否已經建立 – neydroid