Rails 生产数据库中存在无法解释的大量购物车
Unexplained massive number of shopping Carts in Rails database in Production
我使用 Rails 5、postgres 和 heroku 构建了一个电子商务应用程序,其中包含一个使用会话对象存储 cart_id 的经典购物车。感谢 heroku 的警告,我发现我数据库中的购物车数量以一种非常奇怪的方式增加。每分钟可以超过50个;这并不是因为许多客户访问了该站点,该站点目前还很小。其他时刻,Carts的行数table为stable.
这是我的应用程序控制器:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
around_action :switch_locale
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :current_cart
after_action :store_action
def switch_locale(&action)
locale = params[:locale] || I18n.default_locale
I18n.with_locale(locale, &action)
end
def default_url_options
{ locale: I18n.locale == I18n.default_locale ? nil : I18n.locale }
end
def current_cart
if session[:cart_id]
cart = Cart.find_by_id(session[:cart_id])
if cart == nil
session[:cart_id] = nil
end
if cart.present?
@current_cart = cart
else
session[:cart_id] = nil
end
end
if session[:cart_id] == nil
@current_cart = Cart.create
session[:cart_id] = @current_cart.id
end
end
def store_action
return unless request.get?
if (request.path != "/users/sign_in" &&
request.path != "/users/sign_up" &&
request.path != "/users/password/new" &&
request.path != "/users/password/edit" &&
request.path != "/users/confirmation" &&
request.path != "/users/sign_out" &&
!request.xhr?) # don't store ajax calls
store_location_for(:user, request.fullpath)
end
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
end
结束
cart.rb
class Cart < ApplicationRecord
has_many :order_items, dependent: :destroy
has_many :products, through: :order_items
monetize :amount_cents_cents
end
和订单控制器:
class OrdersController < ApplicationController
before_action :authenticate_user!
def create
@order = Order.new
total = []
@current_cart.order_items.each do |item|
total << item.product.price_cents * item.quantity.to_i
end
@order.amount_cents_cents = total.sum
if @order.amount_cents_cents == 0
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
redirect_to root_path
else
@current_cart.order_items.each do |item|
@order.order_items << item
item.cart_id = nil
end
@user = current_user
@order.user_id = @user.id
@order.save
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
redirect_to order_path(@order)
end
这是昨天的 Heroku 日志:
2021-03-09T16:25:31.994631+00:00 app[web.1]: D, [2021-03-09T16:25:31.994568 #4] DEBUG -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] Cart Create (1.1ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES (, ) RETURNING "id" [["created_at", "2021-03-09 16:25:31.993019"], ["updated_at", "2021-03-09 16:25:31.993019"]]
2021-03-09T16:25:31.996727+00:00 app[web.1]: D, [2021-03-09T16:25:31.996649 #4] DEBUG -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] (1.8ms) COMMIT
2021-03-09T16:25:31.998717+00:00 app[web.1]: D, [2021-03-09T16:25:31.998636 #4] DEBUG -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] OrderItem Load (1.1ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = [["cart_id", 102580]]
2021-03-09T16:25:31.999401+00:00 app[web.1]: I, [2021-03-09T16:25:31.999343 #4] INFO -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:25:32.000698+00:00 app[web.1]: I, [2021-03-09T16:25:32.000634 #4] INFO -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] Rendered carts/show.html.erb within layouts/application (1.2ms)
2021-03-09T16:25:32.003572+00:00 app[web.1]: I, [2021-03-09T16:25:32.003492 #4] INFO -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] Rendered shared/_navbar.html.erb (2.0ms)
2021-03-09T16:25:32.003861+00:00 app[web.1]: I, [2021-03-09T16:25:32.003787 #4] INFO -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] Rendered shared/_flashes.html.erb (0.1ms)
2021-03-09T16:25:32.004305+00:00 app[web.1]: I, [2021-03-09T16:25:32.004209 #4] INFO -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] Completed 200 OK in 14ms (Views: 5.0ms | ActiveRecord: 5.0ms)
2021-03-09T16:25:32.006089+00:00 heroku[router]: at=info method=GET path="/en/carts/100155" host=www.bravacoffeeroasters.com request_id=bc4f12a5-b503-435a-bd45-eac3c3adc006 fwd="5.9.108.254" dyno=web.1 connect=0ms service=20ms status=200 bytes=8003 protocol=https
2021-03-09T16:25:39.827045+00:00 app[web.1]: I, [2021-03-09T16:25:39.826944 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Started GET "/en/carts/100158" for 5.9.108.254 at 2021-03-09 16:25:39 +0000
2021-03-09T16:25:39.828020+00:00 app[web.1]: I, [2021-03-09T16:25:39.827942 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Processing by CartsController#show as HTML
2021-03-09T16:25:39.828070+00:00 app[web.1]: I, [2021-03-09T16:25:39.828014 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Parameters: {"locale"=>"en", "id"=>"100158"}
2021-03-09T16:25:39.831671+00:00 app[web.1]: D, [2021-03-09T16:25:39.831585 #4] DEBUG -- : [794133c6-3a8d-4952-b38b-148dd1c78941] (1.2ms) BEGIN
2021-03-09T16:25:39.834022+00:00 app[web.1]: D, [2021-03-09T16:25:39.833931 #4] DEBUG -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Cart Create (1.3ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES (, ) RETURNING "id" [["created_at", "2021-03-09 16:25:39.832057"], ["updated_at", "2021-03-09 16:25:39.832057"]]
2021-03-09T16:25:39.836952+00:00 app[web.1]: D, [2021-03-09T16:25:39.836843 #4] DEBUG -- : [794133c6-3a8d-4952-b38b-148dd1c78941] (2.6ms) COMMIT
2021-03-09T16:25:39.838769+00:00 app[web.1]: D, [2021-03-09T16:25:39.838664 #4] DEBUG -- : [794133c6-3a8d-4952-b38b-148dd1c78941] OrderItem Load (1.1ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = [["cart_id", 102581]]
2021-03-09T16:25:39.839411+00:00 app[web.1]: I, [2021-03-09T16:25:39.839342 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:25:39.840887+00:00 app[web.1]: I, [2021-03-09T16:25:39.840812 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Rendered carts/show.html.erb within layouts/application (1.4ms)
2021-03-09T16:25:39.843830+00:00 app[web.1]: I, [2021-03-09T16:25:39.843767 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Rendered shared/_navbar.html.erb (2.0ms)
2021-03-09T16:25:39.844041+00:00 app[web.1]: I, [2021-03-09T16:25:39.843979 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Rendered shared/_flashes.html.erb (0.1ms)
2021-03-09T16:25:39.844419+00:00 app[web.1]: I, [2021-03-09T16:25:39.844315 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Completed 200 OK in 16ms (Views: 5.1ms | ActiveRecord: 6.2ms)
2021-03-09T16:25:39.846119+00:00 heroku[router]: at=info method=GET path="/en/carts/100158" host=www.bravacoffeeroasters.com request_id=794133c6-3a8d-4952-b38b-148dd1c78941 fwd="5.9.108.254" dyno=web.1 connect=0ms service=21ms status=200 bytes=8007 protocol=https
2021-03-09T16:25:44.393247+00:00 app[web.1]: I, [2021-03-09T16:25:44.393143 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Started GET "/en/carts/100162" for 5.9.108.254 at 2021-03-09 16:25:44 +0000
2021-03-09T16:25:44.394349+00:00 app[web.1]: I, [2021-03-09T16:25:44.394257 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Processing by CartsController#show as HTML
2021-03-09T16:25:44.394397+00:00 app[web.1]: I, [2021-03-09T16:25:44.394331 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Parameters: {"locale"=>"en", "id"=>"100162"}
2021-03-09T16:25:44.402068+00:00 app[web.1]: D, [2021-03-09T16:25:44.401988 #4] DEBUG -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] (1.0ms) BEGIN
2021-03-09T16:25:44.404434+00:00 app[web.1]: D, [2021-03-09T16:25:44.404308 #4] DEBUG -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Cart Create (1.2ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES (, ) RETURNING "id" [["created_at", "2021-03-09 16:25:44.402575"], ["updated_at", "2021-03-09 16:25:44.402575"]]
2021-03-09T16:25:44.406742+00:00 app[web.1]: D, [2021-03-09T16:25:44.406662 #4] DEBUG -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] (1.9ms) COMMIT
2021-03-09T16:25:44.408565+00:00 app[web.1]: D, [2021-03-09T16:25:44.408479 #4] DEBUG -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] OrderItem Load (1.0ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = [["cart_id", 102582]]
2021-03-09T16:25:44.409183+00:00 app[web.1]: I, [2021-03-09T16:25:44.409106 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:25:44.410524+00:00 app[web.1]: I, [2021-03-09T16:25:44.410452 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Rendered carts/show.html.erb within layouts/application (1.2ms)
2021-03-09T16:25:44.413558+00:00 app[web.1]: I, [2021-03-09T16:25:44.413470 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Rendered shared/_navbar.html.erb (2.0ms)
2021-03-09T16:25:44.413840+00:00 app[web.1]: I, [2021-03-09T16:25:44.413772 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Rendered shared/_flashes.html.erb (0.1ms)
2021-03-09T16:25:44.414160+00:00 app[web.1]: I, [2021-03-09T16:25:44.414098 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Completed 200 OK in 20ms (Views: 5.1ms | ActiveRecord: 5.2ms)
2021-03-09T16:25:44.416182+00:00 heroku[router]: at=info method=GET path="/en/carts/100162" host=www.bravacoffeeroasters.com request_id=55da48a9-0088-4a3d-984a-f58f8a81245a fwd="5.9.108.254" dyno=web.1 connect=0ms service=25ms status=200 bytes=8001 protocol=https
2021-03-09T16:25:49.749861+00:00 app[web.1]: I, [2021-03-09T16:25:49.749718 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Started GET "/en/carts/100167" for 5.9.108.254 at 2021-03-09 16:25:49 +0000
2021-03-09T16:25:49.751269+00:00 app[web.1]: I, [2021-03-09T16:25:49.751173 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Processing by CartsController#show as HTML
2021-03-09T16:25:49.751359+00:00 app[web.1]: I, [2021-03-09T16:25:49.751277 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Parameters: {"locale"=>"en", "id"=>"100167"}
2021-03-09T16:25:49.754570+00:00 app[web.1]: D, [2021-03-09T16:25:49.754464 #4] DEBUG -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] (1.1ms) BEGIN
2021-03-09T16:25:49.757801+00:00 app[web.1]: D, [2021-03-09T16:25:49.757693 #4] DEBUG -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Cart Create (1.2ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES (, ) RETURNING "id" [["created_at", "2021-03-09 16:25:49.755581"], ["updated_at", "2021-03-09 16:25:49.755581"]]
2021-03-09T16:25:49.760623+00:00 app[web.1]: D, [2021-03-09T16:25:49.760418 #4] DEBUG -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] (2.2ms) COMMIT
2021-03-09T16:25:49.762818+00:00 app[web.1]: D, [2021-03-09T16:25:49.762617 #4] DEBUG -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] OrderItem Load (1.1ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = [["cart_id", 102583]]
2021-03-09T16:25:49.764416+00:00 app[web.1]: I, [2021-03-09T16:25:49.764303 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:25:49.766505+00:00 app[web.1]: I, [2021-03-09T16:25:49.766423 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Rendered carts/show.html.erb within layouts/application (1.9ms)
2021-03-09T16:25:49.771851+00:00 app[web.1]: I, [2021-03-09T16:25:49.771765 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Rendered shared/_navbar.html.erb (3.8ms)
2021-03-09T16:25:49.772151+00:00 app[web.1]: I, [2021-03-09T16:25:49.772072 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Rendered shared/_flashes.html.erb (0.1ms)
2021-03-09T16:25:49.772669+00:00 app[web.1]: I, [2021-03-09T16:25:49.772589 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Completed 200 OK in 21ms (Views: 8.7ms | ActiveRecord: 5.7ms)
2021-03-09T16:25:49.775098+00:00 heroku[router]: at=info method=GET path="/en/carts/100167" host=www.bravacoffeeroasters.com request_id=c9f138de-a820-4b0b-b87e-ad2f99f6c9f5 fwd="5.9.108.254" dyno=web.1 connect=0ms service=28ms status=200 bytes=8005 protocol=https
2021-03-09T16:25:55.885797+00:00 app[web.1]: I, [2021-03-09T16:25:55.885705 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Started GET "/en/carts/100171" for 5.9.108.254 at 2021-03-09 16:25:55 +0000
2021-03-09T16:25:55.886791+00:00 app[web.1]: I, [2021-03-09T16:25:55.886726 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Processing by CartsController#show as HTML
2021-03-09T16:25:55.886851+00:00 app[web.1]: I, [2021-03-09T16:25:55.886800 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Parameters: {"locale"=>"en", "id"=>"100171"}
2021-03-09T16:25:55.889588+00:00 app[web.1]: D, [2021-03-09T16:25:55.889521 #4] DEBUG -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] (1.0ms) BEGIN
2021-03-09T16:25:55.891685+00:00 app[web.1]: D, [2021-03-09T16:25:55.891611 #4] DEBUG -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Cart Create (1.2ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES (, ) RETURNING "id" [["created_at", "2021-03-09 16:25:55.889991"], ["updated_at", "2021-03-09 16:25:55.889991"]]
2021-03-09T16:25:55.894147+00:00 app[web.1]: D, [2021-03-09T16:25:55.894080 #4] DEBUG -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] (2.1ms) COMMIT
2021-03-09T16:25:55.895883+00:00 app[web.1]: D, [2021-03-09T16:25:55.895820 #4] DEBUG -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] OrderItem Load (1.1ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = [["cart_id", 102584]]
2021-03-09T16:25:55.896595+00:00 app[web.1]: I, [2021-03-09T16:25:55.896512 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:25:55.898042+00:00 app[web.1]: I, [2021-03-09T16:25:55.897982 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Rendered carts/show.html.erb within layouts/application (1.4ms)
2021-03-09T16:25:55.900969+00:00 app[web.1]: I, [2021-03-09T16:25:55.900906 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Rendered shared/_navbar.html.erb (2.0ms)
2021-03-09T16:25:55.901154+00:00 app[web.1]: I, [2021-03-09T16:25:55.901098 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Rendered shared/_flashes.html.erb (0.0ms)
2021-03-09T16:25:55.901439+00:00 app[web.1]: I, [2021-03-09T16:25:55.901385 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Completed 200 OK in 14ms (Views: 5.1ms | ActiveRecord: 5.3ms)
2021-03-09T16:25:55.903393+00:00 heroku[router]: at=info method=GET path="/en/carts/100171" host=www.bravacoffeeroasters.com request_id=e2305a65-52be-432a-867f-ac1c0f459e1b fwd="5.9.108.254" dyno=web.1 connect=0ms service=20ms status=200 bytes=8005 protocol=https
2021-03-09T16:26:02.095206+00:00 app[web.1]: I, [2021-03-09T16:26:02.095107 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Started GET "/en/carts/100176" for 5.9.108.254 at 2021-03-09 16:26:02 +0000
2021-03-09T16:26:02.096223+00:00 app[web.1]: I, [2021-03-09T16:26:02.096121 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Processing by CartsController#show as HTML
2021-03-09T16:26:02.096287+00:00 app[web.1]: I, [2021-03-09T16:26:02.096209 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Parameters: {"locale"=>"en", "id"=>"100176"}
2021-03-09T16:26:02.099292+00:00 app[web.1]: D, [2021-03-09T16:26:02.099195 #4] DEBUG -- : [59ebb736-4614-4052-97dd-9517cfd2af70] (1.0ms) BEGIN
2021-03-09T16:26:02.101864+00:00 app[web.1]: D, [2021-03-09T16:26:02.101787 #4] DEBUG -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Cart Create (1.3ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES (, ) RETURNING "id" [["created_at", "2021-03-09 16:26:02.099815"], ["updated_at", "2021-03-09 16:26:02.099815"]]
2021-03-09T16:26:02.104590+00:00 app[web.1]: D, [2021-03-09T16:26:02.104462 #4] DEBUG -- : [59ebb736-4614-4052-97dd-9517cfd2af70] (2.3ms) COMMIT
2021-03-09T16:26:02.106692+00:00 app[web.1]: D, [2021-03-09T16:26:02.106619 #4] DEBUG -- : [59ebb736-4614-4052-97dd-9517cfd2af70] OrderItem Load (1.3ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = [["cart_id", 102585]]
2021-03-09T16:26:02.107212+00:00 app[web.1]: I, [2021-03-09T16:26:02.107150 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:26:02.108495+00:00 app[web.1]: I, [2021-03-09T16:26:02.108422 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Rendered carts/show.html.erb within layouts/application (1.2ms)
2021-03-09T16:26:02.111100+00:00 app[web.1]: I, [2021-03-09T16:26:02.111034 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Rendered shared/_navbar.html.erb (1.7ms)
2021-03-09T16:26:02.111294+00:00 app[web.1]: I, [2021-03-09T16:26:02.111230 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Rendered shared/_flashes.html.erb (0.1ms)
2021-03-09T16:26:02.111695+00:00 app[web.1]: I, [2021-03-09T16:26:02.111563 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Completed 200 OK in 15ms (Views: 4.5ms | ActiveRecord: 5.9ms)
2021-03-09T16:26:02.114053+00:00 heroku[router]: at=info method=GET path="/en/carts/100176" host=www.bravacoffeeroasters.com request_id=59ebb736-4614-4052-97dd-9517cfd2af70 fwd="5.9.108.254" dyno=web.1 connect=0ms service=21ms status=200 bytes=8011 protocol=https
2021-03-09T16:26:07.168587+00:00 app[web.1]: I, [2021-03-09T16:26:07.168395 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Started GET "/en/carts/100181" for 5.9.108.254 at 2021-03-09 16:26:07 +0000
2021-03-09T16:26:07.169524+00:00 app[web.1]: I, [2021-03-09T16:26:07.169461 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Processing by CartsController#show as HTML
2021-03-09T16:26:07.169585+00:00 app[web.1]: I, [2021-03-09T16:26:07.169533 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Parameters: {"locale"=>"en", "id"=>"100181"}
2021-03-09T16:26:07.172559+00:00 app[web.1]: D, [2021-03-09T16:26:07.172490 #4] DEBUG -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] (1.0ms) BEGIN
2021-03-09T16:26:07.174639+00:00 app[web.1]: D, [2021-03-09T16:26:07.174577 #4] DEBUG -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Cart Create (1.2ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES (, ) RETURNING "id" [["created_at", "2021-03-09 16:26:07.172954"], ["updated_at", "2021-03-09 16:26:07.172954"]]
2021-03-09T16:26:07.176801+00:00 app[web.1]: D, [2021-03-09T16:26:07.176731 #4] DEBUG -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] (1.9ms) COMMIT
2021-03-09T16:26:07.178631+00:00 app[web.1]: D, [2021-03-09T16:26:07.178567 #4] DEBUG -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] OrderItem Load (1.1ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = [["cart_id", 102586]]
2021-03-09T16:26:07.179179+00:00 app[web.1]: I, [2021-03-09T16:26:07.179119 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:26:07.180626+00:00 app[web.1]: I, [2021-03-09T16:26:07.180554 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Rendered carts/show.html.erb within layouts/application (1.3ms)
2021-03-09T16:26:07.183396+00:00 app[web.1]: I, [2021-03-09T16:26:07.183332 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Rendered shared/_navbar.html.erb (1.9ms)
2021-03-09T16:26:07.183592+00:00 app[web.1]: I, [2021-03-09T16:26:07.183524 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Rendered shared/_flashes.html.erb (0.0ms)
2021-03-09T16:26:07.183924+00:00 app[web.1]: I, [2021-03-09T16:26:07.183852 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Completed 200 OK in 14ms (Views: 4.8ms | ActiveRecord: 5.1ms)
2021-03-09T16:26:07.186556+00:00 heroku[router]: at=info method=GET path="/en/carts/100181" host=www.bravacoffeeroasters.com request_id=512ac9cd-da40-435d-a7b8-986ad4209a31 fwd="5.9.108.254" dyno=web.1 connect=0ms service=20ms status=200 bytes=8009 protocol=https
2021-03-09T16:26:12.791059+00:00 app[web.1]: I, [2021-03-09T16:26:12.790956 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Started GET "/en/carts/100185" for 5.9.108.254 at 2021-03-09 16:26:12 +0000
2021-03-09T16:26:12.792005+00:00 app[web.1]: I, [2021-03-09T16:26:12.791938 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Processing by CartsController#show as HTML
2021-03-09T16:26:12.792075+00:00 app[web.1]: I, [2021-03-09T16:26:12.792011 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Parameters: {"locale"=>"en", "id"=>"100185"}
2021-03-09T16:26:12.794860+00:00 app[web.1]: D, [2021-03-09T16:26:12.794781 #4] DEBUG -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] (1.0ms) BEGIN
2021-03-09T16:26:12.797433+00:00 app[web.1]: D, [2021-03-09T16:26:12.797358 #4] DEBUG -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Cart Create (1.2ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES (, ) RETURNING "id" [["created_at", "2021-03-09 16:26:12.795289"], ["updated_at", "2021-03-09 16:26:12.795289"]]
2021-03-09T16:26:12.799742+00:00 app[web.1]: D, [2021-03-09T16:26:12.799657 #4] DEBUG -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] (1.8ms) COMMIT
2021-03-09T16:26:12.801691+00:00 app[web.1]: D, [2021-03-09T16:26:12.801618 #4] DEBUG -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] OrderItem Load (1.2ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = [["cart_id", 102587]]
2021-03-09T16:26:12.802222+00:00 app[web.1]: I, [2021-03-09T16:26:12.802153 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:26:12.803798+00:00 app[web.1]: I, [2021-03-09T16:26:12.803727 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Rendered carts/show.html.erb within layouts/application (1.5ms)
2021-03-09T16:26:12.807236+00:00 app[web.1]: I, [2021-03-09T16:26:12.807070 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Rendered shared/_navbar.html.erb (2.3ms)
2021-03-09T16:26:12.807516+00:00 app[web.1]: I, [2021-03-09T16:26:12.807446 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Rendered shared/_flashes.html.erb (0.1ms)
2021-03-09T16:26:12.807908+00:00 app[web.1]: I, [2021-03-09T16:26:12.807840 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Completed 200 OK in 16ms (Views: 5.8ms | ActiveRecord: 5.2ms)
2021-03-09T16:26:12.809202+00:00 heroku[router]: at=info method=GET path="/en/carts/100185" host=www.bravacoffeeroasters.com request_id=ee78de9f-ffb9-4c3b-a484-8438903fd9c9 fwd="5.9.108.254" dyno=web.1 connect=0ms service=20ms status=200 bytes=8013 protocol=https
我昨天删除了生产数据库的所有购物车,今天我已经有3000多行了。这对我来说听起来很糟糕。有人可以帮助解决问题吗?
如果我正确阅读了您的代码:
before_action :current_cart
def current_cart
...
if session[:cart_id] == nil
@current_cart = Cart.create
session[:cart_id] = @current_cart.id
end
end
在 ApplicationController
中添加的 before_action 将针对继承自 ApplicationController
的每个控制器中的每个操作执行。
甚至你的主页也可能执行它。当 session[:cart_id]
为零时(根据您当前的问题,这种情况经常发生)每次访问都会创建一个 Cart
.
IMO current_cart
的目的与 current_user
相同,是寻找购物车而不是创建购物车。如果它不存在,那么创建它是其他操作的目的。
你应该稍微修改一下你的逻辑。祝你好运!
我使用 Rails 5、postgres 和 heroku 构建了一个电子商务应用程序,其中包含一个使用会话对象存储 cart_id 的经典购物车。感谢 heroku 的警告,我发现我数据库中的购物车数量以一种非常奇怪的方式增加。每分钟可以超过50个;这并不是因为许多客户访问了该站点,该站点目前还很小。其他时刻,Carts的行数table为stable.
这是我的应用程序控制器:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
around_action :switch_locale
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :current_cart
after_action :store_action
def switch_locale(&action)
locale = params[:locale] || I18n.default_locale
I18n.with_locale(locale, &action)
end
def default_url_options
{ locale: I18n.locale == I18n.default_locale ? nil : I18n.locale }
end
def current_cart
if session[:cart_id]
cart = Cart.find_by_id(session[:cart_id])
if cart == nil
session[:cart_id] = nil
end
if cart.present?
@current_cart = cart
else
session[:cart_id] = nil
end
end
if session[:cart_id] == nil
@current_cart = Cart.create
session[:cart_id] = @current_cart.id
end
end
def store_action
return unless request.get?
if (request.path != "/users/sign_in" &&
request.path != "/users/sign_up" &&
request.path != "/users/password/new" &&
request.path != "/users/password/edit" &&
request.path != "/users/confirmation" &&
request.path != "/users/sign_out" &&
!request.xhr?) # don't store ajax calls
store_location_for(:user, request.fullpath)
end
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
end
结束
cart.rb
class Cart < ApplicationRecord
has_many :order_items, dependent: :destroy
has_many :products, through: :order_items
monetize :amount_cents_cents
end
和订单控制器:
class OrdersController < ApplicationController
before_action :authenticate_user!
def create
@order = Order.new
total = []
@current_cart.order_items.each do |item|
total << item.product.price_cents * item.quantity.to_i
end
@order.amount_cents_cents = total.sum
if @order.amount_cents_cents == 0
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
redirect_to root_path
else
@current_cart.order_items.each do |item|
@order.order_items << item
item.cart_id = nil
end
@user = current_user
@order.user_id = @user.id
@order.save
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
redirect_to order_path(@order)
end
这是昨天的 Heroku 日志:
2021-03-09T16:25:31.994631+00:00 app[web.1]: D, [2021-03-09T16:25:31.994568 #4] DEBUG -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] Cart Create (1.1ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES (, ) RETURNING "id" [["created_at", "2021-03-09 16:25:31.993019"], ["updated_at", "2021-03-09 16:25:31.993019"]]
2021-03-09T16:25:31.996727+00:00 app[web.1]: D, [2021-03-09T16:25:31.996649 #4] DEBUG -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] (1.8ms) COMMIT
2021-03-09T16:25:31.998717+00:00 app[web.1]: D, [2021-03-09T16:25:31.998636 #4] DEBUG -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] OrderItem Load (1.1ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = [["cart_id", 102580]]
2021-03-09T16:25:31.999401+00:00 app[web.1]: I, [2021-03-09T16:25:31.999343 #4] INFO -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:25:32.000698+00:00 app[web.1]: I, [2021-03-09T16:25:32.000634 #4] INFO -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] Rendered carts/show.html.erb within layouts/application (1.2ms)
2021-03-09T16:25:32.003572+00:00 app[web.1]: I, [2021-03-09T16:25:32.003492 #4] INFO -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] Rendered shared/_navbar.html.erb (2.0ms)
2021-03-09T16:25:32.003861+00:00 app[web.1]: I, [2021-03-09T16:25:32.003787 #4] INFO -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] Rendered shared/_flashes.html.erb (0.1ms)
2021-03-09T16:25:32.004305+00:00 app[web.1]: I, [2021-03-09T16:25:32.004209 #4] INFO -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] Completed 200 OK in 14ms (Views: 5.0ms | ActiveRecord: 5.0ms)
2021-03-09T16:25:32.006089+00:00 heroku[router]: at=info method=GET path="/en/carts/100155" host=www.bravacoffeeroasters.com request_id=bc4f12a5-b503-435a-bd45-eac3c3adc006 fwd="5.9.108.254" dyno=web.1 connect=0ms service=20ms status=200 bytes=8003 protocol=https
2021-03-09T16:25:39.827045+00:00 app[web.1]: I, [2021-03-09T16:25:39.826944 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Started GET "/en/carts/100158" for 5.9.108.254 at 2021-03-09 16:25:39 +0000
2021-03-09T16:25:39.828020+00:00 app[web.1]: I, [2021-03-09T16:25:39.827942 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Processing by CartsController#show as HTML
2021-03-09T16:25:39.828070+00:00 app[web.1]: I, [2021-03-09T16:25:39.828014 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Parameters: {"locale"=>"en", "id"=>"100158"}
2021-03-09T16:25:39.831671+00:00 app[web.1]: D, [2021-03-09T16:25:39.831585 #4] DEBUG -- : [794133c6-3a8d-4952-b38b-148dd1c78941] (1.2ms) BEGIN
2021-03-09T16:25:39.834022+00:00 app[web.1]: D, [2021-03-09T16:25:39.833931 #4] DEBUG -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Cart Create (1.3ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES (, ) RETURNING "id" [["created_at", "2021-03-09 16:25:39.832057"], ["updated_at", "2021-03-09 16:25:39.832057"]]
2021-03-09T16:25:39.836952+00:00 app[web.1]: D, [2021-03-09T16:25:39.836843 #4] DEBUG -- : [794133c6-3a8d-4952-b38b-148dd1c78941] (2.6ms) COMMIT
2021-03-09T16:25:39.838769+00:00 app[web.1]: D, [2021-03-09T16:25:39.838664 #4] DEBUG -- : [794133c6-3a8d-4952-b38b-148dd1c78941] OrderItem Load (1.1ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = [["cart_id", 102581]]
2021-03-09T16:25:39.839411+00:00 app[web.1]: I, [2021-03-09T16:25:39.839342 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:25:39.840887+00:00 app[web.1]: I, [2021-03-09T16:25:39.840812 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Rendered carts/show.html.erb within layouts/application (1.4ms)
2021-03-09T16:25:39.843830+00:00 app[web.1]: I, [2021-03-09T16:25:39.843767 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Rendered shared/_navbar.html.erb (2.0ms)
2021-03-09T16:25:39.844041+00:00 app[web.1]: I, [2021-03-09T16:25:39.843979 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Rendered shared/_flashes.html.erb (0.1ms)
2021-03-09T16:25:39.844419+00:00 app[web.1]: I, [2021-03-09T16:25:39.844315 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Completed 200 OK in 16ms (Views: 5.1ms | ActiveRecord: 6.2ms)
2021-03-09T16:25:39.846119+00:00 heroku[router]: at=info method=GET path="/en/carts/100158" host=www.bravacoffeeroasters.com request_id=794133c6-3a8d-4952-b38b-148dd1c78941 fwd="5.9.108.254" dyno=web.1 connect=0ms service=21ms status=200 bytes=8007 protocol=https
2021-03-09T16:25:44.393247+00:00 app[web.1]: I, [2021-03-09T16:25:44.393143 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Started GET "/en/carts/100162" for 5.9.108.254 at 2021-03-09 16:25:44 +0000
2021-03-09T16:25:44.394349+00:00 app[web.1]: I, [2021-03-09T16:25:44.394257 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Processing by CartsController#show as HTML
2021-03-09T16:25:44.394397+00:00 app[web.1]: I, [2021-03-09T16:25:44.394331 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Parameters: {"locale"=>"en", "id"=>"100162"}
2021-03-09T16:25:44.402068+00:00 app[web.1]: D, [2021-03-09T16:25:44.401988 #4] DEBUG -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] (1.0ms) BEGIN
2021-03-09T16:25:44.404434+00:00 app[web.1]: D, [2021-03-09T16:25:44.404308 #4] DEBUG -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Cart Create (1.2ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES (, ) RETURNING "id" [["created_at", "2021-03-09 16:25:44.402575"], ["updated_at", "2021-03-09 16:25:44.402575"]]
2021-03-09T16:25:44.406742+00:00 app[web.1]: D, [2021-03-09T16:25:44.406662 #4] DEBUG -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] (1.9ms) COMMIT
2021-03-09T16:25:44.408565+00:00 app[web.1]: D, [2021-03-09T16:25:44.408479 #4] DEBUG -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] OrderItem Load (1.0ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = [["cart_id", 102582]]
2021-03-09T16:25:44.409183+00:00 app[web.1]: I, [2021-03-09T16:25:44.409106 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:25:44.410524+00:00 app[web.1]: I, [2021-03-09T16:25:44.410452 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Rendered carts/show.html.erb within layouts/application (1.2ms)
2021-03-09T16:25:44.413558+00:00 app[web.1]: I, [2021-03-09T16:25:44.413470 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Rendered shared/_navbar.html.erb (2.0ms)
2021-03-09T16:25:44.413840+00:00 app[web.1]: I, [2021-03-09T16:25:44.413772 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Rendered shared/_flashes.html.erb (0.1ms)
2021-03-09T16:25:44.414160+00:00 app[web.1]: I, [2021-03-09T16:25:44.414098 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Completed 200 OK in 20ms (Views: 5.1ms | ActiveRecord: 5.2ms)
2021-03-09T16:25:44.416182+00:00 heroku[router]: at=info method=GET path="/en/carts/100162" host=www.bravacoffeeroasters.com request_id=55da48a9-0088-4a3d-984a-f58f8a81245a fwd="5.9.108.254" dyno=web.1 connect=0ms service=25ms status=200 bytes=8001 protocol=https
2021-03-09T16:25:49.749861+00:00 app[web.1]: I, [2021-03-09T16:25:49.749718 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Started GET "/en/carts/100167" for 5.9.108.254 at 2021-03-09 16:25:49 +0000
2021-03-09T16:25:49.751269+00:00 app[web.1]: I, [2021-03-09T16:25:49.751173 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Processing by CartsController#show as HTML
2021-03-09T16:25:49.751359+00:00 app[web.1]: I, [2021-03-09T16:25:49.751277 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Parameters: {"locale"=>"en", "id"=>"100167"}
2021-03-09T16:25:49.754570+00:00 app[web.1]: D, [2021-03-09T16:25:49.754464 #4] DEBUG -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] (1.1ms) BEGIN
2021-03-09T16:25:49.757801+00:00 app[web.1]: D, [2021-03-09T16:25:49.757693 #4] DEBUG -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Cart Create (1.2ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES (, ) RETURNING "id" [["created_at", "2021-03-09 16:25:49.755581"], ["updated_at", "2021-03-09 16:25:49.755581"]]
2021-03-09T16:25:49.760623+00:00 app[web.1]: D, [2021-03-09T16:25:49.760418 #4] DEBUG -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] (2.2ms) COMMIT
2021-03-09T16:25:49.762818+00:00 app[web.1]: D, [2021-03-09T16:25:49.762617 #4] DEBUG -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] OrderItem Load (1.1ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = [["cart_id", 102583]]
2021-03-09T16:25:49.764416+00:00 app[web.1]: I, [2021-03-09T16:25:49.764303 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:25:49.766505+00:00 app[web.1]: I, [2021-03-09T16:25:49.766423 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Rendered carts/show.html.erb within layouts/application (1.9ms)
2021-03-09T16:25:49.771851+00:00 app[web.1]: I, [2021-03-09T16:25:49.771765 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Rendered shared/_navbar.html.erb (3.8ms)
2021-03-09T16:25:49.772151+00:00 app[web.1]: I, [2021-03-09T16:25:49.772072 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Rendered shared/_flashes.html.erb (0.1ms)
2021-03-09T16:25:49.772669+00:00 app[web.1]: I, [2021-03-09T16:25:49.772589 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Completed 200 OK in 21ms (Views: 8.7ms | ActiveRecord: 5.7ms)
2021-03-09T16:25:49.775098+00:00 heroku[router]: at=info method=GET path="/en/carts/100167" host=www.bravacoffeeroasters.com request_id=c9f138de-a820-4b0b-b87e-ad2f99f6c9f5 fwd="5.9.108.254" dyno=web.1 connect=0ms service=28ms status=200 bytes=8005 protocol=https
2021-03-09T16:25:55.885797+00:00 app[web.1]: I, [2021-03-09T16:25:55.885705 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Started GET "/en/carts/100171" for 5.9.108.254 at 2021-03-09 16:25:55 +0000
2021-03-09T16:25:55.886791+00:00 app[web.1]: I, [2021-03-09T16:25:55.886726 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Processing by CartsController#show as HTML
2021-03-09T16:25:55.886851+00:00 app[web.1]: I, [2021-03-09T16:25:55.886800 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Parameters: {"locale"=>"en", "id"=>"100171"}
2021-03-09T16:25:55.889588+00:00 app[web.1]: D, [2021-03-09T16:25:55.889521 #4] DEBUG -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] (1.0ms) BEGIN
2021-03-09T16:25:55.891685+00:00 app[web.1]: D, [2021-03-09T16:25:55.891611 #4] DEBUG -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Cart Create (1.2ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES (, ) RETURNING "id" [["created_at", "2021-03-09 16:25:55.889991"], ["updated_at", "2021-03-09 16:25:55.889991"]]
2021-03-09T16:25:55.894147+00:00 app[web.1]: D, [2021-03-09T16:25:55.894080 #4] DEBUG -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] (2.1ms) COMMIT
2021-03-09T16:25:55.895883+00:00 app[web.1]: D, [2021-03-09T16:25:55.895820 #4] DEBUG -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] OrderItem Load (1.1ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = [["cart_id", 102584]]
2021-03-09T16:25:55.896595+00:00 app[web.1]: I, [2021-03-09T16:25:55.896512 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:25:55.898042+00:00 app[web.1]: I, [2021-03-09T16:25:55.897982 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Rendered carts/show.html.erb within layouts/application (1.4ms)
2021-03-09T16:25:55.900969+00:00 app[web.1]: I, [2021-03-09T16:25:55.900906 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Rendered shared/_navbar.html.erb (2.0ms)
2021-03-09T16:25:55.901154+00:00 app[web.1]: I, [2021-03-09T16:25:55.901098 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Rendered shared/_flashes.html.erb (0.0ms)
2021-03-09T16:25:55.901439+00:00 app[web.1]: I, [2021-03-09T16:25:55.901385 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Completed 200 OK in 14ms (Views: 5.1ms | ActiveRecord: 5.3ms)
2021-03-09T16:25:55.903393+00:00 heroku[router]: at=info method=GET path="/en/carts/100171" host=www.bravacoffeeroasters.com request_id=e2305a65-52be-432a-867f-ac1c0f459e1b fwd="5.9.108.254" dyno=web.1 connect=0ms service=20ms status=200 bytes=8005 protocol=https
2021-03-09T16:26:02.095206+00:00 app[web.1]: I, [2021-03-09T16:26:02.095107 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Started GET "/en/carts/100176" for 5.9.108.254 at 2021-03-09 16:26:02 +0000
2021-03-09T16:26:02.096223+00:00 app[web.1]: I, [2021-03-09T16:26:02.096121 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Processing by CartsController#show as HTML
2021-03-09T16:26:02.096287+00:00 app[web.1]: I, [2021-03-09T16:26:02.096209 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Parameters: {"locale"=>"en", "id"=>"100176"}
2021-03-09T16:26:02.099292+00:00 app[web.1]: D, [2021-03-09T16:26:02.099195 #4] DEBUG -- : [59ebb736-4614-4052-97dd-9517cfd2af70] (1.0ms) BEGIN
2021-03-09T16:26:02.101864+00:00 app[web.1]: D, [2021-03-09T16:26:02.101787 #4] DEBUG -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Cart Create (1.3ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES (, ) RETURNING "id" [["created_at", "2021-03-09 16:26:02.099815"], ["updated_at", "2021-03-09 16:26:02.099815"]]
2021-03-09T16:26:02.104590+00:00 app[web.1]: D, [2021-03-09T16:26:02.104462 #4] DEBUG -- : [59ebb736-4614-4052-97dd-9517cfd2af70] (2.3ms) COMMIT
2021-03-09T16:26:02.106692+00:00 app[web.1]: D, [2021-03-09T16:26:02.106619 #4] DEBUG -- : [59ebb736-4614-4052-97dd-9517cfd2af70] OrderItem Load (1.3ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = [["cart_id", 102585]]
2021-03-09T16:26:02.107212+00:00 app[web.1]: I, [2021-03-09T16:26:02.107150 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:26:02.108495+00:00 app[web.1]: I, [2021-03-09T16:26:02.108422 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Rendered carts/show.html.erb within layouts/application (1.2ms)
2021-03-09T16:26:02.111100+00:00 app[web.1]: I, [2021-03-09T16:26:02.111034 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Rendered shared/_navbar.html.erb (1.7ms)
2021-03-09T16:26:02.111294+00:00 app[web.1]: I, [2021-03-09T16:26:02.111230 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Rendered shared/_flashes.html.erb (0.1ms)
2021-03-09T16:26:02.111695+00:00 app[web.1]: I, [2021-03-09T16:26:02.111563 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Completed 200 OK in 15ms (Views: 4.5ms | ActiveRecord: 5.9ms)
2021-03-09T16:26:02.114053+00:00 heroku[router]: at=info method=GET path="/en/carts/100176" host=www.bravacoffeeroasters.com request_id=59ebb736-4614-4052-97dd-9517cfd2af70 fwd="5.9.108.254" dyno=web.1 connect=0ms service=21ms status=200 bytes=8011 protocol=https
2021-03-09T16:26:07.168587+00:00 app[web.1]: I, [2021-03-09T16:26:07.168395 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Started GET "/en/carts/100181" for 5.9.108.254 at 2021-03-09 16:26:07 +0000
2021-03-09T16:26:07.169524+00:00 app[web.1]: I, [2021-03-09T16:26:07.169461 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Processing by CartsController#show as HTML
2021-03-09T16:26:07.169585+00:00 app[web.1]: I, [2021-03-09T16:26:07.169533 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Parameters: {"locale"=>"en", "id"=>"100181"}
2021-03-09T16:26:07.172559+00:00 app[web.1]: D, [2021-03-09T16:26:07.172490 #4] DEBUG -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] (1.0ms) BEGIN
2021-03-09T16:26:07.174639+00:00 app[web.1]: D, [2021-03-09T16:26:07.174577 #4] DEBUG -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Cart Create (1.2ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES (, ) RETURNING "id" [["created_at", "2021-03-09 16:26:07.172954"], ["updated_at", "2021-03-09 16:26:07.172954"]]
2021-03-09T16:26:07.176801+00:00 app[web.1]: D, [2021-03-09T16:26:07.176731 #4] DEBUG -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] (1.9ms) COMMIT
2021-03-09T16:26:07.178631+00:00 app[web.1]: D, [2021-03-09T16:26:07.178567 #4] DEBUG -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] OrderItem Load (1.1ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = [["cart_id", 102586]]
2021-03-09T16:26:07.179179+00:00 app[web.1]: I, [2021-03-09T16:26:07.179119 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:26:07.180626+00:00 app[web.1]: I, [2021-03-09T16:26:07.180554 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Rendered carts/show.html.erb within layouts/application (1.3ms)
2021-03-09T16:26:07.183396+00:00 app[web.1]: I, [2021-03-09T16:26:07.183332 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Rendered shared/_navbar.html.erb (1.9ms)
2021-03-09T16:26:07.183592+00:00 app[web.1]: I, [2021-03-09T16:26:07.183524 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Rendered shared/_flashes.html.erb (0.0ms)
2021-03-09T16:26:07.183924+00:00 app[web.1]: I, [2021-03-09T16:26:07.183852 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Completed 200 OK in 14ms (Views: 4.8ms | ActiveRecord: 5.1ms)
2021-03-09T16:26:07.186556+00:00 heroku[router]: at=info method=GET path="/en/carts/100181" host=www.bravacoffeeroasters.com request_id=512ac9cd-da40-435d-a7b8-986ad4209a31 fwd="5.9.108.254" dyno=web.1 connect=0ms service=20ms status=200 bytes=8009 protocol=https
2021-03-09T16:26:12.791059+00:00 app[web.1]: I, [2021-03-09T16:26:12.790956 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Started GET "/en/carts/100185" for 5.9.108.254 at 2021-03-09 16:26:12 +0000
2021-03-09T16:26:12.792005+00:00 app[web.1]: I, [2021-03-09T16:26:12.791938 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Processing by CartsController#show as HTML
2021-03-09T16:26:12.792075+00:00 app[web.1]: I, [2021-03-09T16:26:12.792011 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Parameters: {"locale"=>"en", "id"=>"100185"}
2021-03-09T16:26:12.794860+00:00 app[web.1]: D, [2021-03-09T16:26:12.794781 #4] DEBUG -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] (1.0ms) BEGIN
2021-03-09T16:26:12.797433+00:00 app[web.1]: D, [2021-03-09T16:26:12.797358 #4] DEBUG -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Cart Create (1.2ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES (, ) RETURNING "id" [["created_at", "2021-03-09 16:26:12.795289"], ["updated_at", "2021-03-09 16:26:12.795289"]]
2021-03-09T16:26:12.799742+00:00 app[web.1]: D, [2021-03-09T16:26:12.799657 #4] DEBUG -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] (1.8ms) COMMIT
2021-03-09T16:26:12.801691+00:00 app[web.1]: D, [2021-03-09T16:26:12.801618 #4] DEBUG -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] OrderItem Load (1.2ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = [["cart_id", 102587]]
2021-03-09T16:26:12.802222+00:00 app[web.1]: I, [2021-03-09T16:26:12.802153 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:26:12.803798+00:00 app[web.1]: I, [2021-03-09T16:26:12.803727 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Rendered carts/show.html.erb within layouts/application (1.5ms)
2021-03-09T16:26:12.807236+00:00 app[web.1]: I, [2021-03-09T16:26:12.807070 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Rendered shared/_navbar.html.erb (2.3ms)
2021-03-09T16:26:12.807516+00:00 app[web.1]: I, [2021-03-09T16:26:12.807446 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Rendered shared/_flashes.html.erb (0.1ms)
2021-03-09T16:26:12.807908+00:00 app[web.1]: I, [2021-03-09T16:26:12.807840 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Completed 200 OK in 16ms (Views: 5.8ms | ActiveRecord: 5.2ms)
2021-03-09T16:26:12.809202+00:00 heroku[router]: at=info method=GET path="/en/carts/100185" host=www.bravacoffeeroasters.com request_id=ee78de9f-ffb9-4c3b-a484-8438903fd9c9 fwd="5.9.108.254" dyno=web.1 connect=0ms service=20ms status=200 bytes=8013 protocol=https
我昨天删除了生产数据库的所有购物车,今天我已经有3000多行了。这对我来说听起来很糟糕。有人可以帮助解决问题吗?
如果我正确阅读了您的代码:
before_action :current_cart
def current_cart
...
if session[:cart_id] == nil
@current_cart = Cart.create
session[:cart_id] = @current_cart.id
end
end
在 ApplicationController
中添加的 before_action 将针对继承自 ApplicationController
的每个控制器中的每个操作执行。
甚至你的主页也可能执行它。当 session[:cart_id]
为零时(根据您当前的问题,这种情况经常发生)每次访问都会创建一个 Cart
.
IMO current_cart
的目的与 current_user
相同,是寻找购物车而不是创建购物车。如果它不存在,那么创建它是其他操作的目的。
你应该稍微修改一下你的逻辑。祝你好运!