Commit b5aeeccd by Hoang Phuc Do

N + 1 query fix

parent 03678484
......@@ -93,6 +93,8 @@ group :development do
gem 'spring-watcher-listen', '~> 2.0.0'
# When mail is sent from your application, Letter Opener will open a preview in the browser instead of sending.
gem 'letter_opener', '~> 1.4', '>= 1.4.1'
# help to kill N+1 queries and unused eager loading.
gem 'bullet', '~> 5.5', '>= 5.5.1'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
......
......@@ -54,6 +54,9 @@ GEM
autoprefixer-rails (>= 5.2.1)
sass (>= 3.3.4)
builder (3.2.3)
bullet (5.5.1)
activesupport (>= 3.0.0)
uniform_notifier (~> 1.10.0)
byebug (9.0.6)
capybara (2.14.0)
addressable
......@@ -287,6 +290,7 @@ GEM
uglifier (3.2.0)
execjs (>= 0.3.0, < 3)
unicode-display_width (1.2.1)
uniform_notifier (1.10.0)
warden (1.2.7)
rack (>= 1.0)
web-console (3.5.1)
......@@ -307,6 +311,7 @@ PLATFORMS
DEPENDENCIES
active_link_to (~> 1.0, >= 1.0.4)
bootstrap-sass (~> 3.3.6)
bullet (~> 5.5, >= 5.5.1)
byebug
capybara (~> 2.13)
carrierwave (~> 1.0)
......
......@@ -8,7 +8,7 @@ class AdminController < ApplicationController
# GET /admin/orders
def orders
@orders = Order.page(params[:page]).per(5)
@orders = Order.includes(:product_items).page(params[:page]).per(5)
end
private
......
class CategoriesController < ApplicationController
def show
@category = Category.find(params[:id])
@products = @category.products.page(params[:page]).per(5)
@products = @category.products.includes(:user).page(params[:page]).per(5)
end
end
\ No newline at end of file
class StaticPagesController < ApplicationController
def index
@latest_products = Product.page(params[:page]).per(5)
@latest_products = Product.includes(:user).page(params[:page]).per(5)
# @TODO: Get recommended products
@recommended_products = Product.last(6)
# session.clear
......
......@@ -13,6 +13,6 @@ class UsersController < ApplicationController
# GET /users/orders
def orders
@orders = current_user.orders.page(params[:page]).per(5)
@orders = current_user.orders.includes(:product_items).page(params[:page]).per(5)
end
end
\ No newline at end of file
......@@ -10,7 +10,7 @@ class Product < ApplicationRecord
validates :user_id, presence: true
def in_stock?(required_quantity)
return false if required_quantity <= 0
return false if required_quantity.to_i < 0
quantity >= required_quantity.to_i
end
......
......@@ -19,6 +19,6 @@ class Search
def matched_products(products)
product_ids = products.map { |product| product[:id.to_s] }
Product.find(product_ids)
Product.includes(:user).find(product_ids)
end
end
\ No newline at end of file
......@@ -53,4 +53,13 @@ Rails.application.configure do
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
config.action_mailer.delivery_method = :letter_opener
config.after_initialize do
Bullet.enable = true
Bullet.alert = true
Bullet.bullet_logger = true
Bullet.console = true
Bullet.rails_logger = true
Bullet.add_footer = true
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment