Page loads unnecessary queries Ruby On Rails - ruby-on-rails
I have a weird problem, every page of my website that contains database queries loads some extra items. I've never had this problem before, tried to check all controllers but haven't found any problems. Here is an example of output when I load (or refresh) home page:
=> Booting Puma
=> Rails 5.1.6 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.0 (ruby 2.5.0-p0), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
Started GET "/c/wines" for 127.0.0.1 at 2018-11-02 15:55:30 -0400 (0.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by CatalogsController#show as HTML
Parameters: {"id"=>"wines"}
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."slug" = ? LIMIT ? [["slug", "wines"], ["LIMIT", 1]]
Rendering catalogs/show.html.haml within layouts/application (0.2ms) SELECT COUNT(*) FROM "categories" WHERE "categories"."catalog_id" = ? [["catalog_id", 1]] (0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Category Exists (0.3ms) SELECT 1 AS one FROM "categories" WHERE "categories"."catalog_id" = 1 AND "categories"."open" = ? LIMIT ? [["open", "t"], ["LIMIT", 1]]
Category Load (0.9ms) SELECT "categories".* FROM "categories" WHERE "categories"."catalog_id" = 1 AND "categories"."open" = ? [["open", "t"]]
CACHE Category Load (0.0ms) SELECT "categories".* FROM "categories" WHERE "categories"."catalog_id" = 1 AND "categories"."open" = ? [["open", "t"]]
CACHE Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."catalog_id" = 1 AND "categories"."open" = ? [["open", "t"]]
Product Load (0.8ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 1 AND "products"."open" = ? AND "products"."category_id" = 1 [["open", "t"]]
Catalog Load (0.7ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 1
Product Load (0.3ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 1 AND "products"."open" = ? AND "products"."category_id" = 2 [["open", "t"]]
CACHE Catalog Load (0.0ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 1
Rendered catalogs/show.html.haml within layouts/application (142.7ms)
Rendered application/_favicon.html.haml (11.1ms)
Catalog Load (0.4ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_navbar.html.haml (23.7ms)
CACHE Catalog Load (0.0ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_footer.html.haml (15.7ms)
Completed 200 OK in 937ms (Views: 890.8ms | ActiveRecord: 9.4ms)
Started GET "/uploads/catalog/photo/1/hero-8.png" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Started GET "/" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Processing by StaticController#home as HTML
Started GET "/uploads/product/photo/4/pc_pistachio.png" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Started GET "/uploads/product/photo/7/pc_pistachio.png" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Started GET "/uploads/product/photo/1/pc_creame.png" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Started GET "/uploads/product/photo/10/pc_pistachio.png" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Rendering static/home.html.haml within layouts/application
Catalog Load (0.3ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? ORDER BY created_at ASC LIMIT ? [["open", "t"], ["LIMIT", 6]]
(0.3ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
CACHE (0.0ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Product Exists (0.4ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 1]]
Product Load (0.9ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 1
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (1, 2)
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 2
Product Exists (0.2ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 1]]
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 2
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 3
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 3
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 1]]
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 3
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 2
Rendered static/home.html.haml within layouts/application (95.0ms)
Rendered application/_favicon.html.haml (15.0ms)
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_navbar.html.haml (19.9ms)
CACHE Catalog Load (0.0ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_footer.html.haml (18.0ms)
Completed 200 OK in 400ms (Views: 389.1ms | ActiveRecord: 4.3ms)
Started GET "/" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Started GET "/" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Processing by StaticController#home as HTML
Started GET "/" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Started GET "/" for 127.0.0.1 at 2018-11-02 15:55:31 -0400
Processing by StaticController#home as HTML
Rendering static/home.html.haml within layouts/application
Processing by StaticController#home as HTML
Catalog Load (0.5ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? ORDER BY created_at ASC LIMIT ? [["open", "t"], ["LIMIT", 6]]
Rendering static/home.html.haml within layouts/application
Processing by StaticController#home as HTML
Catalog Load (0.4ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? ORDER BY created_at ASC LIMIT ? [["open", "t"], ["LIMIT", 6]]
Rendering static/home.html.haml within layouts/application
(0.3ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
CACHE (0.0ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 1]]
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? ORDER BY created_at ASC LIMIT ? [["open", "t"], ["LIMIT", 6]]
(0.1ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 1
CACHE (0.0ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Product Exists (0.2ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 1]]
Rendering static/home.html.haml within layouts/application
(0.4ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? ORDER BY created_at ASC LIMIT ? [["open", "t"], ["LIMIT", 6]]
Product Load (0.3ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 6]]
(0.1ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
CACHE (0.0ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (1, 2)
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 1
CACHE (0.0ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Product Exists (0.2ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 1]]
Category Load (0.3ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (1, 2)
Product Load (0.4ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 6]]
Product Exists (0.4ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 1]]
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 2
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 1]]
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 2
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 3
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 3
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 1]]
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 2
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 1
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 6]]
Product Exists (0.4ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 1]]
Product Load (0.3ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 2
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 1
Product Load (0.4ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 6]]
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (1, 2)
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 2
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (1, 2)
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 2
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 1]]
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 2
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 3
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 3
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 1]]
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 3
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 1]]
Product Load (0.3ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 3
Catalog Load (0.4ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 3
Product Load (0.6ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 6]]
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 3
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 1]]
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 3
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 2
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 2
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 3
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 2
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 2
Rendered static/home.html.haml within layouts/application (206.4ms)
Rendered application/_favicon.html.haml (6.5ms)
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 3
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 1]]
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 3
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 2
Rendered static/home.html.haml within layouts/application (195.5ms)
Rendered application/_favicon.html.haml (0.1ms)
Rendered static/home.html.haml within layouts/application (246.3ms)
Rendered static/home.html.haml within layouts/application (255.3ms)
Rendered application/_favicon.html.haml (0.1ms)
Rendered application/_favicon.html.haml (0.0ms)
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_navbar.html.haml (13.9ms)
CACHE Catalog Load (0.0ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_footer.html.haml (9.7ms)
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_navbar.html.haml (3.2ms)
Catalog Load (0.3ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_navbar.html.haml (3.2ms)
Rendered shared/_navbar.html.haml (14.3ms)
CACHE Catalog Load (0.0ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
CACHE Catalog Load (0.0ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
CACHE Catalog Load (0.0ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_footer.html.haml (5.9ms)
Rendered shared/_footer.html.haml (3.0ms)
Rendered shared/_footer.html.haml (6.0ms)
Completed 200 OK in 859ms (Views: 853.1ms | ActiveRecord: 3.5ms)
Completed 200 OK in 802ms (Views: 795.7ms | ActiveRecord: 4.0ms)
Completed 200 OK in 849ms (Views: 842.5ms | ActiveRecord: 3.7ms)
Started GET "/" for 127.0.0.1 at 2018-11-02 15:55:32 -0400
Processing by StaticController#home as HTML
Completed 200 OK in 858ms (Views: 851.2ms | ActiveRecord: 3.8ms)
Rendering static/home.html.haml within layouts/application
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? ORDER BY created_at ASC LIMIT ? [["open", "t"], ["LIMIT", 6]]
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
CACHE (0.0ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Product Exists (0.3ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 1]]
Product Load (0.6ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.4ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 1
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (1, 2)
(0.5ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 2
Product Exists (0.2ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 1]]
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 2
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 3
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 3
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 1]]
Product Load (0.4ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.3ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 3
Category Load (0.5ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 2
Rendered static/home.html.haml within layouts/application (58.7ms)
Rendered application/_favicon.html.haml (9.7ms)
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_navbar.html.haml (16.9ms)
CACHE Catalog Load (0.0ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_footer.html.haml (11.7ms)
Completed 200 OK in 261ms (Views: 254.4ms | ActiveRecord: 4.7ms)
My Static controller:
class StaticController < ApplicationController
def home
#catalogs = Catalog.all.where(open: true).order('created_at ASC').limit(6)
# #products = Product.all.includes(:catalog, :category).where(open: true).order('created_at DESC').take(8)
end
def about
#catalogs = Catalog.all.where(open: true)
#categories = Category.all.where(open: true)
#products = Product.all.where(open: true)
end
def download_pdf
send_file "#{Rails.root}/app/assets/docs/1_s.pdf", type: "application/pdf", x_sendfile: true
end
end
Note: I have similar problems with other controllers
At the beginning I thought that it's a bag because I didn't add any new code. Please, let me know if you need more code from controllers. Thank you very much for your time and help.
UPDATE
Application Controller:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :prepare_meta_tags, if: "request.get?"
def prepare_meta_tags(options={})
site_name = "Alvian Imports Inc."
title = "Alvain Imports Inc."
description = "For many years, Alvian imports alcohol accross Florida state and now it's time to grow. Absolutly new team with new products that will be loved by everyone. We have an exclusive partnership with Ponche Caribe and we are starting to deliver our products to Texas, California, Mexico, and Columbia. Our goal is to make alcohol importation better and faster, so our team is working hard to deliver wonderful products to your closest stores."
image = options[:image] || "app/assets/images/brand/logo.png"
current_url = request.url
# Let's prepare a nice set of defaults
defaults = {
site: site_name,
title: title,
image: image,
description: description,
keywords: %w[food food processor food service food and drink f&b food industry food and beverage service food & beverage food processing industry food and beverage manager food and beverage industry food service jobs food beverage beverage industry food service companies f&b service food service worker food industry jobs food service industry food & drink food drink food and beverage management types of food service f&b industry food and beverage department import imports alvian alvianimports miami usa florida],
twitter: {
site_name: site_name,
site: '#IncAlvian',
card: 'The Beverage Company. Ponche Caribe Curacao Blue & Liquors Exclusive Distributor Florida - Texas - California - Colombia Distributor. Must be 21 +',
description: description,
image: image
},
og: {
url: current_url,
site_name: site_name,
title: title,
image: image,
description: description,
type: 'website'
}
}
options.reverse_merge!(defaults)
set_meta_tags options
end
end
Application.html.haml code:
!!!
%html(lang="en-US" class="region--#{controller.controller_name}")
%head
= display_meta_tags
%meta(http-equiv="X-UA-Compatible" content="IE=edge,chrome=1")
%meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
%meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
%meta{:charset => "utf-8"}/
%meta{:content => "width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no", :name => "viewport"}/
%meta{:content => "user-scalable=no, width=device-width, initial-scale=1.0", :name => "viewport"}/
%meta{:content => "yes", :name => "apple-mobile-web-app-capable"}/
%meta{:'http-equiv' => "X-UA-Compatible", :content => "IE=edge"}
%meta{:name => "HandheldFriendly", :contnet => "true"}
%meta{:name => "MobileOptimized", :content => "176"}
= render 'application/favicon'
%title #{content_for?(:title) ? yield(:title) : 'Alvian Imports'}
= csrf_meta_tags
= stylesheet_link_tag 'application', media: 'all'
= analytics_init if GoogleAnalytics.valid_tracker?
%body(class = '#{controller.controller_name}')
%div(class="application-wrapper")
= render partial: 'shared/navbar'
= content_tag :main, class: "layout layout-#{controller.action_name}" do
= yield
= render partial: 'shared/footer'
- unless params[:nojs]
= javascript_include_tag 'application'
UPDATE 2
I have 3 models: Catalog, Category, and Products.
Catalog model:
class Catalog < ApplicationRecord
extend FriendlyId
friendly_id :name, use: [:slugged, :history]
mount_uploader :photo, PhotoUploader
has_many :categories, dependent: :delete_all
has_many :products, :through => :categories, :source => :catalog, dependent: :delete_all
# Important fields
validates_presence_of :name
end
Category model:
class Category < ApplicationRecord
extend FriendlyId
friendly_id :name, use: [:slugged, :history]
belongs_to :catalog, :foreign_key => 'catalog_id'
has_many :products, dependent: :delete_all
# Important fields
validates_presence_of :name
def self.active
where(status: 1)
end
end
Product model:
class Product < ApplicationRecord
extend FriendlyId
friendly_id :name, use: [:slugged, :history]
mount_uploader :photo, PhotoUploader
belongs_to :catalog, :foreign_key => 'catalog_id'
belongs_to :category, :foreign_key => 'category_id'
# Important fields
validates_presence_of :name
end
Expected output in the console:
Started GET "/" for 127.0.0.1 at 2018-11-02 16:41:26 -0400
Processing by StaticController#home as HTML
Rendering static/home.html.haml within layouts/application
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? ORDER BY created_at ASC LIMIT ? [["open", "t"], ["LIMIT", 6]]
(0.1ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
CACHE (0.0ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 1
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 1]]
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 1 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 1
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (1, 2)
(0.2ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 2
Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 1]]
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 2 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.1ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 2
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 3
(0.3ms) SELECT COUNT(*) FROM "products" WHERE "products"."catalog_id" = 3
Product Exists (0.3ms) SELECT 1 AS one FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 1]]
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."catalog_id" = 3 LIMIT ? [["LIMIT", 6]]
Catalog Load (0.3ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."id" = 3
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 2
Rendered static/home.html.haml within layouts/application (44.4ms)
Rendered application/_favicon.html.haml (7.9ms)
Catalog Load (0.2ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_navbar.html.haml (15.8ms)
CACHE Catalog Load (0.0ms) SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."open" = ? [["open", "t"]]
Rendered shared/_footer.html.haml (8.9ms)
Completed 200 OK in 218ms (Views: 213.2ms | ActiveRecord: 3.1ms)
Home page HTML (HAML), however it has to be mentioned that I get similar problem with other pages that requires queries (for example catalog show pages):
= content_for :navbar_class, "navbar-dark"
%section(class="hero hero-homepage")
%div(class="hh-container")
%div(class="hh-carousel")
- #catalogs.each do |catalog|
%div(class="hhc-item")
= link_to catalog, class: 'hhc-container' do
%div(class="hhc-background" style="background-image: url(#{catalog.photo});")
-# = image_tag(catalog.photo, lazy: true, class: "hhc-background")
%div(class="hhc-caption")
%h4(class="heading heading-1 is-light") #{catalog.name}
- if !catalog.description.empty?
%p(class="text text-2 is-light is-transparent") #{truncate(catalog.description, :length => 112)}
%section(class="section")
- #catalogs.each do |pc|
- #products = Product.all.where(catalog_id: pc)
%div(class="content")
%div(class="header header-hr")
%span
%h4(class="heading heading-4") New #{pc.name}
- if #products.count > 6
%span
= link_to "View all " + #products.count.to_s, pc, class: "link link-secondary link-arrow"
%div(class="row")
- if !#products.empty?
- #products.includes(:catalog, :category).take(6).each do |p|
%div(class="col-xs-6 col-md-4 col-lg-3")
= link_to catalog_category_product_url(p.catalog, p.category, p), class: "block block-product" do
= image_tag p.photo, alt: p.name, class: 'bp-photo', lazy: true
%div(class="bp-desc")
%span
%h5(class="heading heading-5") #{p.name}
%span
%p(class="text text-3")
%span.is-primary #{p.catalog.name}
%span - #{p.category.name}
- else
%div(class="col-xs-12 is-centered")
%h5(class="heading heading-5") Currently this list is empty.
UPDATE 3
Ok, I didn't understand the course of the problem but I decided to recreate the database:
rake db:drop db:create db:migrate
Fixed the problem..
A whole bunch of things and hard to help without actually running the code.
First of all, familiarize yourself with good practice in regards of naming things.
#catalogs.each do |pc| is a no-no and should be #catalogs.each do |catalog|.
if !#products.empty? and #product.count, those trigger two queries and my recommendation is to write the count into #products_count and replace that empty? with #product_count > 0
Use scope in the model to replace things like .where(open: true)
In #products.includes(:catalog, :category) you can remove the catalog because you already walk the products by catalog and can use pc (or as I recommended above: catalog) instead of product.catalog
If catalog_category_product_url doesn't use anything fancy but just the ids (e.g. catalog/5/category/2/product/23) then you can do catalog_category_product_url(product.catalog_id, product.category_id, product) to prevent a few extra queries (especially if you forgot to use eager loading or don't want to load a big chunk of data from the db).
Related
Speeding up rails queries
I've got a table that contains 10k products. When I query the database after a server restart, the server is querying all of the 10k products and then limits to 15 records in the response. Is this normal? It is causing my page to take 23 seconds for the first page to load. Upon the second request, everything looks better, but I still don't understand why the first query is grabbing all of those records. My Query: #products = Product.limit(15) The first response: Started GET "/admin/admin_products/" for 50.255.94.246 at 2017-05-31 17:43:49 +0000 Cannot render console from 50.255.94.246! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 ActiveRecord::SchemaMigration Load (0.6ms) SELECT "schema_migrations".* FROM "schema_migrations" Processing by AdminProductsController#index as HTML /home/ubuntu/workspace/app/models/product.rb:96: warning: key :description is duplicated and overwritten on line 96 Product Load (5.7ms) SELECT "products".* FROM "products" ORDER BY "products"."id" ASC LIMIT $1 [["LIMIT", 1000]] Product Load (12.2ms) SELECT "products".* FROM "products" WHERE ("products"."id" > 1000) ORDER BY "products"."id" ASC LIMIT $1 [["LIMIT", 1000]] Product Load (6.4ms) SELECT "products".* FROM "products" WHERE ("products"."id" > 2000) ORDER BY "products"."id" ASC LIMIT $1 [["LIMIT", 1000]] Product Load (9.6ms) SELECT "products".* FROM "products" WHERE ("products"."id" > 3000) ORDER BY "products"."id" ASC LIMIT $1 [["LIMIT", 1000]] Product Load (9.2ms) SELECT "products".* FROM "products" WHERE ("products"."id" > 4000) ORDER BY "products"."id" ASC LIMIT $1 [["LIMIT", 1000]] Product Load (9.1ms) SELECT "products".* FROM "products" WHERE ("products"."id" > 5000) ORDER BY "products"."id" ASC LIMIT $1 [["LIMIT", 1000]] Product Load (9.9ms) SELECT "products".* FROM "products" WHERE ("products"."id" > 6000) ORDER BY "products"."id" ASC LIMIT $1 [["LIMIT", 1000]] Product Load (9.4ms) SELECT "products".* FROM "products" WHERE ("products"."id" > 7000) ORDER BY "products"."id" ASC LIMIT $1 [["LIMIT", 1000]] Product Load (59.5ms) SELECT "products".* FROM "products" WHERE ("products"."id" > 8000) ORDER BY "products"."id" ASC LIMIT $1 [["LIMIT", 1000]] Product Load (131.0ms) SELECT "products".* FROM "products" WHERE ("products"."id" > 9000) ORDER BY "products"."id" ASC LIMIT $1 [["LIMIT", 1000]] Product Load (2.1ms) SELECT "products".* FROM "products" WHERE ("products"."id" > 10000) ORDER BY "products"."id" ASC LIMIT $1 [["LIMIT", 1000]] Rendering admin_products/index.html.erb within layouts/application Rendered layouts/_admin_portal.html.erb (0.6ms) Product Load (0.8ms) SELECT "products".* FROM "products" LIMIT $1 [["LIMIT", 15]] Rendered collection of admin_products/_product.html.erb [15 times] (2.8ms) Rendered admin_products/_paginate.html.erb (0.6ms) Rendered admin_products/index.html.erb within layouts/application (15.9ms) Rendered layouts/_flash_messages.html.erb (0.8ms) Completed 200 OK in 23486ms (Views: 7643.7ms | ActiveRecord: 269.2ms) The second time I execute the query: Started GET "/admin/admin_products/" for 50.255.94.246 at 2017-05-31 17:59:00 +0000 Cannot render console from 50.255.94.246! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by AdminProductsController#index as HTML Rendering admin_products/index.html.erb within layouts/application Rendered layouts/_admin_portal.html.erb (0.5ms) Product Load (0.9ms) SELECT "products".* FROM "products" LIMIT $1 [["LIMIT", 15]] Rendered collection of admin_products/_product.html.erb [15 times] (2.7ms) Rendered admin_products/_paginate.html.erb (0.6ms) Rendered admin_products/index.html.erb within layouts/application (10.8ms) Rendered layouts/_flash_messages.html.erb (0.8ms) Completed 200 OK in 26ms (Views: 23.5ms | ActiveRecord: 0.9ms)
#SteveTurczyn pointed out my issue which ended up being rather simple. The problem involved calling an import function for Elastic Search which was in my Product model. After I removed the line of code, everything worked as expected. Here is the code that slowed down the process. Product.includes(normal_model: [:normal_brand]).import force: true I now have a controller method that invokes the import function when needed.
How do I get my Rails 5 log to show me as many details about each operation like Rails 4 did?
For some reason, even in development, my Rails 5 log only shows me some information. I am not sure how it chooses what to show. I don't know if it is because I have Turbolinks installed, but I doubt it. Was there a change in the way Rails logs requests in development in Rails 5 over 4? Edit 1 This is hard to show fully here, but here is an example. I restarted my server, then I visited my root_path which is questions#index. Then I went to an individual question question#show, then I refreshed that show page. This is what the log shows of those 3 requests: $ rails s => Booting Puma => Rails 5.0.0.rc1 application starting in development on http://localhost:3000 => Run `rails server -h` for more startup options Puma starting in single mode... * Version 3.4.0 (ruby 2.3.0-p0), codename: Owl Bowl Brawl * Min threads: 5, max threads: 5 * Environment: development * Listening on tcp://localhost:3000 Use Ctrl-C to stop Started GET "/" for ::1 at 2016-05-16 16:08:03 -0500 ActiveRecord::SchemaMigration Load (1.0ms) SELECT "schema_migrations".* FROM "schema_migrations" Processing by QuestionsController#index as HTML Rendering questions/index.html.erb within layouts/application Rendered shared/_main_page_heading.html.erb (0.3ms) Question Load (1.1ms) SELECT "questions".* FROM "questions" User Load (1.5ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (1521, 1522, 1523, 1524, 1525, 1526, 1527, 1528, 1529, 1530, 1531, 1532, 1533, 1534, 1535, 1536, 1537, 1538) (1.1ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 4]] CACHE (0.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 4]] Rendered questions/_question.html.erb (443.5ms) (1.9ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 5]] CACHE (0.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 5]] Rendered questions/_question.html.erb (5.2ms) (1.7ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 6]] CACHE (0.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 6]] Rendered questions/_question.html.erb (4.9ms) (1.9ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 7]] CACHE (0.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 7]] Rendered questions/_question.html.erb (5.0ms) (1.8ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 8]] CACHE (0.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 8]] Rendered questions/_question.html.erb (5.2ms) (1.7ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 9]] CACHE (0.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 9]] Rendered questions/_question.html.erb (5.0ms) (1.7ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 10]] CACHE (0.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 10]] Rendered questions/_question.html.erb (5.4ms) (2.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 11]] CACHE (0.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 11]] Rendered questions/_question.html.erb (5.7ms) (1.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 12]] CACHE (0.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 12]] Rendered questions/_question.html.erb (3.2ms) (1.6ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 13]] CACHE (0.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 13]] Rendered questions/_question.html.erb (5.0ms) (1.1ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 14]] CACHE (0.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 14]] Rendered questions/_question.html.erb (4.8ms) (1.4ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 15]] CACHE (0.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 15]] Rendered questions/_question.html.erb (4.9ms) (2.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 16]] CACHE (0.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 16]] Rendered questions/_question.html.erb (7.4ms) (2.2ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 17]] CACHE (0.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 17]] Rendered questions/_question.html.erb (6.6ms) (2.1ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 18]] CACHE (0.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 18]] Rendered questions/_question.html.erb (6.3ms) (1.1ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 19]] CACHE (0.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 19]] Rendered questions/_question.html.erb (3.9ms) (1.5ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 20]] CACHE (0.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 20]] Rendered questions/_question.html.erb (6.1ms) (1.2ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 21]] CACHE (0.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 21]] Rendered questions/_question.html.erb (3.6ms) Rendered questions/index.html.erb within layouts/application (621.0ms) Rendered shared/_navbar.html.erb (1.1ms) Completed 200 OK in 1917ms (Views: 1844.9ms | ActiveRecord: 52.9ms) It only shows me the first operation, not the 2nd 2. Say I stop the server, while still on /questions/4, then restart it and refresh and then go back to the home page, this is what the log shows: $ rails s => Booting Puma => Rails 5.0.0.rc1 application starting in development on http://localhost:3000 => Run `rails server -h` for more startup options Puma starting in single mode... * Version 3.4.0 (ruby 2.3.0-p0), codename: Owl Bowl Brawl * Min threads: 5, max threads: 5 * Environment: development * Listening on tcp://localhost:3000 Use Ctrl-C to stop Started GET "/questions/4" for ::1 at 2016-05-16 16:10:55 -0500 ActiveRecord::SchemaMigration Load (1.4ms) SELECT "schema_migrations".* FROM "schema_migrations" Processing by QuestionsController#show as HTML Parameters: {"id"=>"4"} Question Load (1.1ms) SELECT "questions".* FROM "questions" WHERE "questions"."id" = $1 LIMIT $2 [["id", 4], ["LIMIT", 1]] Rendering questions/show.html.erb within layouts/application User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1521], ["LIMIT", 1]] (2.2ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 4]] CACHE (0.0ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 4]] Answer Load (2.8ms) SELECT "answers".* FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 4]] User Load (3.1ms) SELECT "users".* FROM "users" ORDER BY RANDOM() LIMIT $1 [["LIMIT", 1]] Rendered answers/_form.html.erb (535.9ms) Rendered questions/show.html.erb within layouts/application (637.2ms) Rendered shared/_navbar.html.erb (1.2ms) Completed 200 OK in 2666ms (Views: 2575.6ms | ActiveRecord: 34.4ms) It doesn't show the 2nd request, just the first. It could be because it is being cached or maybe Turbolinks is serving up the last version of the page from my cache or something, but the point is that I am not seeing the request hit my server at all. Not sure how to get all 2 or 3 requests to show up in my log. It is hard to show something not showing up, but I hope this explains it a bit better.
After having the same problem using rails 5 rc1 and puma 3.4.0 I figured out the problem has to do with quiet_assets gem having thread-safe issues. So your options are: Set an env variable RAILS_MAX_THREADS=1 to run just one thread locally. Or delete quiet_assets from your Gemfile and have a messy log until quiet assets be finally implemented to rails 5. UPDATE sprockets-rails has finally added the config.assets.quiet option. So the final solution is to update sprockets-rails (and rails 5 also as there is finally a stable version) and add: #config/environments/development.rb config.assets.quiet = true
You can control it by setting the appropriate log level in your config/environments/.rb file, e.g.: config.log_level = :debug
extract multiple params from url
I have a rails app and I'd like to extract 2 different ids from this url: http://localhost:3000/shopping/suppliers/2/products/1 Looking at the development.log it seems like both values are there. I'm wondering how I can expose these params to the controller, is it just params[:supplier_id] and params[:id]? Started GET "/shopping/suppliers/2/products/1" for 127.0.0.1 at 2016-04-22 18:28:19 +1000 Processing by Shopping::ProductsController#show as HTML Parameters: {"supplier_id"=>"2", "id"=>"1"} Product Load (30.1ms) SELECT "products".* FROM "products" WHERE "products"."permalink" = $1 ORDER BY "products"."id" ASC LIMIT 1 [["permalink", "1"]] Product Load (0.8ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT 1 [["id", 1]] User Load (19.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] CACHE (0.0ms) SELECT "products".* FROM "products" WHERE "products"."permalink" = $1 ORDER BY "products"."id" ASC LIMIT 1 [["permalink", "1"]] CACHE (0.0ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT 1 [["id", "1"]] Rendered shopping/products/show.html.haml within layouts/mdl (0.3ms) Rendered shared/_meta_data.html.haml (0.8ms) Rendered shared/_top_cart.html.haml (0.1ms) Rendered shared/_compact_menu.html.haml (4.5ms) Completed 200 OK in 109ms (Views: 46.2ms | ActiveRecord: 50.8ms)
Yes, you should be able to access your values with those keys: params[:supplier_id] params[:id] It is wierd why you have not tried your own suggestion before posting your question. :)
Stop infinite caching of a single object in rails
I have a simple rails app that was running fine till yesterday. But it is showing very unique behavior now by caching the same object again and again because of which the page is not getting loaded. When I hit the Url I get this in development console and it keeps going on and on. Started GET "/store" for 127.0.0.1 at 2016-02-02 12:26:27 +0530 Started GET "/store" for 127.0.0.1 at 2016-02-02 12:26:27 +0530 ActiveRecord::SchemaMigration Load (0.5ms) SELECT "schema_migrations".* FROM "schema_migrations" ActiveRecord::SchemaMigration Load (0.5ms) SELECT "schema_migrations".* FROM "schema_migrations" Processing by WelcomeController#store as HTML Processing by WelcomeController#store as HTML Category Load (1.7ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 Category Load (1.7ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 Category Load (0.9ms) SELECT "categories".* FROM "categories" Category Load (0.9ms) SELECT "categories".* FROM "categories" Rendered welcome/store.html.erb within layouts/application (28.4ms) Rendered welcome/store.html.erb within layouts/application (28.4ms) CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" CACHE (0.0ms) SELECT "categories".* FROM "categories" Rendered welcome/store.html.erb within layouts/application (13.5ms) Rendered welcome/store.html.erb within layouts/application (13.5ms) CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" CACHE (0.0ms) SELECT "categories".* FROM "categories" Rendered welcome/store.html.erb within layouts/application (14.5ms) Rendered welcome/store.html.erb within layouts/application (14.5ms) CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" CACHE (0.0ms) SELECT "categories".* FROM "categories" Rendered welcome/store.html.erb within layouts/application (12.8ms) Rendered welcome/store.html.erb within layouts/application (12.8ms) CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" CACHE (0.0ms) SELECT "categories".* FROM "categories" Rendered welcome/store.html.erb within layouts/application (13.1ms) Rendered welcome/store.html.erb within layouts/application (13.1ms) CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" CACHE (0.0ms) SELECT "categories".* FROM "categories" Rendered welcome/store.html.erb within layouts/application (16.7ms) Rendered welcome/store.html.erb within layouts/application (16.7ms) CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" CACHE (0.0ms) SELECT "categories".* FROM "categories" Rendered welcome/store.html.erb within layouts/application (14.8ms) Rendered welcome/store.html.erb within layouts/application (14.8ms) CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" CACHE (0.0ms) SELECT "categories".* FROM "categories" Rendered welcome/store.html.erb within layouts/application (13.0ms) Rendered welcome/store.html.erb within layouts/application (13.0ms) CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" CACHE (0.0ms) SELECT "categories".* FROM "categories" Rendered welcome/store.html.erb within layouts/application (14.3ms) Rendered welcome/store.html.erb within layouts/application (14.3ms) CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" CACHE (0.0ms) SELECT "categories".* FROM "categories" Rendered welcome/store.html.erb within layouts/application (11.3ms) Rendered welcome/store.html.erb within layouts/application (11.3ms) CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" CACHE (0.0ms) SELECT "categories".* FROM "categories" Rendered welcome/store.html.erb within layouts/application (15.1ms) Rendered welcome/store.html.erb within layouts/application (15.1ms) CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" CACHE (0.0ms) SELECT "categories".* FROM "categories" Rendered welcome/store.html.erb within layouts/application (11.2ms) Rendered welcome/store.html.erb within layouts/application (11.2ms) CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" CACHE (0.0ms) SELECT "categories".* FROM "categories" Rendered welcome/store.html.erb within layouts/application (13.0ms) Rendered welcome/store.html.erb within layouts/application (13.0ms) CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1 CACHE (0.0ms) SELECT "categories".* FROM "categories" CACHE (0.0ms) SELECT "categories".* FROM "categories" This keeps going on and on. I have written these three lines to fetch categories/category in my action for two different objects which I need. #categories = Category.all #category = Category.first #category = Category.where(:search_index => params[:s_i], :keyword => params[:key]).first if params[:s_i] I have not even enabled caching in my development.rb config.action_controller.perform_caching = false The strange thing is that it is running fine onn my staging server on heroku. I have tried the following commands but nothing seems to help. Rails.cache.clear rake tmp:cache:clear I will be really very thankful if someone helps me understand what is going wrong. I am using Rails 4.2.5 and Ruby 2.2.3 on Ubuntu 14.04
n + 1 issue when ordering by child's attribute
The following query suffers from the 'n+1' problem of loading each order for each record: Job.joins('LEFT JOIN orders ON orders.job_id = jobs.id').order("orders.featured") Same for this: Job.includes(:order).order("orders.featured") Removing the .order(...) part removes the n + 1 issue, but then it's not ordered. Any ideas how to fix this? Do I need to create a column in the parent for the 'featured' attribute? Output: Order Load (0.4ms) SELECT "orders".* FROM "orders" WHERE "orders"."job_id" = $1 LIMIT 1 [["job_id", 26]] Rendered jobs/_job.html.erb (1.9ms) Order Load (0.3ms) SELECT "orders".* FROM "orders" WHERE "orders"."job_id" = $1 LIMIT 1 [["job_id", 3]] Rendered jobs/_job.html.erb (2.0ms) Order Load (0.3ms) SELECT "orders".* FROM "orders" WHERE "orders"."job_id" = $1 LIMIT 1 [["job_id", 52]] Rendered jobs/_job.html.erb (1.7ms) Order Load (0.3ms) SELECT "orders".* FROM "orders" WHERE "orders"."job_id" = $1 LIMIT 1 [["job_id", 13]] Rendered jobs/_job.html.erb (1.9ms) Order Load (0.3ms) SELECT "orders".* FROM "orders" WHERE "orders"."job_id" = $1 LIMIT 1 [["job_id", 34]] Rendered jobs/_job.html.erb (1.9ms) Order Load (0.4ms) SELECT "orders".* FROM "orders" WHERE "orders"."job_id" = $1 LIMIT 1 [["job_id", 64]] Rendered jobs/_job.html.erb (2.8ms) Order Load (0.4ms) SELECT "orders".* FROM "orders" WHERE "orders"."job_id" = $1 LIMIT 1 [["job_id", 94]] Rendered jobs/_job.html.erb (3.2ms) Order Load (0.4ms) SELECT "orders".* FROM "orders" WHERE "orders"."job_id" = $1 LIMIT 1 [["job_id", 60]] Rendered jobs/_job.html.erb (3.1ms) Order Load (0.4ms) SELECT "orders".* FROM "orders" WHERE "orders"."job_id" = $1 LIMIT 1 [["job_id", 29]]
Try using preload for the associated data: Job.joins('LEFT JOIN orders ON orders.job_id = jobs.id') .order("orders.featured") .preload(:orders)
Job.includes(:order).order("orders.featured") Includes doesn't join the tables together until you call it in the view. It will actually do 2 queries. If you want the 2 tables to be joined to do the order, you need to use eager_load: Job.eager_load(:order).order("orders.featured") http://blog.bigbinary.com/2013/07/01/preload-vs-eager-load-vs-joins-vs-includes.html