extract multiple params from url - ruby-on-rails

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. :)

Related

Page loads unnecessary queries 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).

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.

Spree Print Invoice Blank

I am using Spree 3.0.0, Spree Print Invoice gem and I have set it up in my gemfile using the branch: '3-0-stable'.
I have installed it with the command
bundle && exec rails g spree_print_invoice:install
I have a simple config/initializers/print_invoice.rb
Spree::PrintInvoice::Config.set(page_layout: :portrait,page_size: 'A4')
Spree::PrintInvoice::Config.set(print_buttons: 'invoice')
Spree::PrintInvoice::Config.set(store_pdf: true) # Default: tmp/order_prints
I have now in my admin configuration a Print Invoice Settings section as well as a print invoice button with each orders.
Here is the log when creating invoice:
Started GET "/admin/orders/R123456789.pdf?template=invoice" for ::1 at 2015-07-22 17:10:00 -0500
Processing by Spree::Admin::OrdersController#show as PDF
Parameters: {"template"=>"invoice", "id"=>"R123456789"}
Spree::Preference Load (0.3ms) SELECT "spree_preferences".* FROM "spree_preferences" WHERE "spree_preferences"."key" = $1 LIMIT 1 [["key", "spree/backend_configuration/locale"]]
Spree::User Load (0.5ms) SELECT "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" = $1 ORDER BY "spree_users"."id" ASC LIMIT 1 [["id", 6]]
(0.4ms) SELECT COUNT(*) FROM "spree_roles" INNER JOIN "spree_roles_users" ON "spree_roles"."id" = "spree_roles_users"."role_id" WHERE "spree_roles_users"."user_id" = $1 AND "spree_roles"."name" = $2 [["user_id", 6], ["name", "admin"]]
Spree::Order Load (0.6ms) SELECT "spree_orders".* FROM "spree_orders" WHERE "spree_orders"."number" = $1 ORDER BY "spree_orders"."id" ASC LIMIT 1 [["number", "R123456789"]]
Spree::Adjustment Load (0.5ms) SELECT "spree_adjustments".* FROM "spree_adjustments" WHERE "spree_adjustments"."adjustable_type" = 'Spree::Order' AND "spree_adjustments"."adjustable_id" IN (1) ORDER BY spree_adjustments.created_at ASC
Rendered text template (0.0ms)
Sent data (2.7ms)
Completed 200 OK in 18ms (Views: 2.5ms | ActiveRecord: 2.3ms)
The invoices are created into tmp/order_prints but they all are completely blanks. The pdf viewer of my browser is blocked on loading.
Anything I need to do in order to make it work?

Cannot update status of table specific instance because rollback is taking place

I have a table subscription with a column status. In my subscriptions controller I have a method accept_player that is supposed to update the subscription.status to "confirmed!"
def accept_player
#subscription = Subscription.find(params[:subscription_id_accept_player])
#subscription.status = "confirmed!"
#subscription.save
authorize #subscription
redirect_to tournament_subscriptions_path(#subscription.tournament)
end
unfortunately every time I try to trigger that method, a rollback seem to take place:
Started POST "/accept_player/39" for ::1 at 2015-07-08 22:01:21 +0100
ActiveRecord::SchemaMigration Load (12.4ms) SELECT "schema_migrations".* FROM "schema_migrations"
/Users/davidgeismar/code/davidgeismar/tennis-match/app/controllers/subscriptions_controller.rb:141: warning: duplicated key at line 155 ignored: "CardType"
Processing by SubscriptionsController#accept_player as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"aas8OPHBpvPwNbbmx/SVipsRM+eKo63nuVilMroxKcU9HRVonjSqEuH7aLY91gFi9PHMUsUqRqk7qhnv2m4L/A==", "subscription_id_accept_player"=>"39", "commit"=>"Confirmer ce Joueur", "subscription_id"=>"39"}
User Load (13.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 2]]
Subscription Load (11.6ms) SELECT "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."id" = $1 LIMIT 1 [["id", 39]]
(5.7ms) BEGIN
Subscription Exists (0.8ms) SELECT 1 AS one FROM "subscriptions" WHERE ("subscriptions"."user_id" = 20 AND "subscriptions"."id" != 39 AND "subscriptions"."tournament_id" = 9) LIMIT 1
(12.6ms) ROLLBACK
Tournament Load (2.4ms) SELECT "tournaments".* FROM "tournaments" WHERE "tournaments"."id" = $1 LIMIT 1 [["id", 9]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 2]]
Redirected to http://localhost:3000/tournaments/9/subscriptions
Completed 302 Found in 246ms (ActiveRecord: 79.7ms)
Any ideas about what might be going wrong here ?
This code:
authorize #subscription
is probably causing the rollback. If you're in dev mode, just comment it out, reload!, and try to manually add a record and see if that's the cause.

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

Resources