Getting an error in postgresql where column name cannot be found? - ruby-on-rails

I am getting the following error in my Heroku logs when a query in my rails application is being invoked :
2019-03-19T02:16:25.782434+00:00 app[web.1]: I, [2019-03-19T02:16:25.782337 #4] INFO -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec] Started GET "/patients?utf8=
%E2%9C%93&Full_Name=tester" for 95.45.117.131 at 2019-03-19 02:16:25 +0000
2019-03-19T02:16:25.783395+00:00 app[web.1]: I, [2019-03-19T02:16:25.783337 #4] INFO -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec] Processing by PatientsContro
ller#index as HTML
2019-03-19T02:16:25.783570+00:00 app[web.1]: I, [2019-03-19T02:16:25.783521 #4] INFO -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec] Parameters: {"utf8"=>"✓",
"Full_Name"=>"tester"}
2019-03-19T02:16:25.790532+00:00 app[web.1]: D, [2019-03-19T02:16:25.790467 #4] DEBUG -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec] User Load (3.7ms) SELECT
"users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 5], ["LIMIT", 1]]
2019-03-19T02:16:25.794827+00:00 app[web.1]: D, [2019-03-19T02:16:25.794753 #4] DEBUG -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec] (3.2ms) SELECT COUNT(*)
FROM "patients" WHERE (full_name LIKE '%tester%' AND user_id = 5)
2019-03-19T02:16:25.795077+00:00 app[web.1]: I, [2019-03-19T02:16:25.795025 #4] INFO -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec] Completed 500 Internal Serve
r Error in 11ms (ActiveRecord: 6.9ms)
2019-03-19T02:16:25.795954+00:00 app[web.1]: F, [2019-03-19T02:16:25.795899 #4] FATAL -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec]
2019-03-19T02:16:25.796028+00:00 app[web.1]: F, [2019-03-19T02:16:25.795980 #4] FATAL -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec] ActiveRecord::StatementInval
id (PG::UndefinedColumn: ERROR: column "full_name" does not exist
2019-03-19T02:16:25.796030+00:00 app[web.1]: LINE 1: SELECT COUNT(*) FROM "patients" WHERE (full_name LIKE '%test...
2019-03-19T02:16:25.796032+00:00 app[web.1]: ^
2019-03-19T02:16:25.796033+00:00 app[web.1]: HINT: Perhaps you meant to reference the column "patients.Full_Name".
2019-03-19T02:16:25.796035+00:00 app[web.1]: : SELECT COUNT(*) FROM "patients" WHERE (full_name LIKE '%tester%' AND user_id = 5)):
2019-03-19T02:16:25.796102+00:00 app[web.1]: F, [2019-03-19T02:16:25.796057 #4] FATAL -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec]
2019-03-19T02:16:25.796180+00:00 app[web.1]: F, [2019-03-19T02:16:25.796138 #4] FATAL -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec] app/controllers/patients_con
troller.rb:15:in `index'
2019-03-19T02:16:25.796617+00:00 heroku[router]: at=info method=GET path="/patients?utf8=%E2%9C%93&Full_Name=tester" host=pure-reef-79084.herokuapp.com reques
t_id=75c3a8ff-f6cb-4954-8f23-071bfc2671ec fwd="95.45.117.131" dyno=web.1 connect=0ms service=16ms status=500 bytes=1827 protocol=https
the code being executed is this :
patients_controller.rb
#patients = Patient.where("patients.Full_Name LIKE :name AND patients.user_id = :id", {:name => "%#{params[:Full_Name]}%", :id => current_user.id})
I have narrowed it down to the fact that postgresql is case sensitive so i am assuming it does not like "Full_Name". I tried encapsulating the column name in double quotes like this :
#patients = Patient.where(""Full_Name" LIKE :name AND user_id = :id", {:name => "%#{params[:Full_Name]}%", :id => current_user.id})
however I get a SyntaxError. Anyone have any suggestions how i would amend this query so that it will fetch the column?
Solved
As per the answer below I had to put quotes around my column name like so :
#patients = Patient.where('"Full_Name" LIKE :name AND patients.user_id = :id', {:name => "%#{params[:Full_Name]}%", :id => current_user.id})

Your table was probably created with double quotes around the column name "Full_Name" so it expects that exact capitalization. All your references to this column should be exactly like that, not Full_Name or "full_name".
Either reference it properly, or rename your column to omit the double quotes:
ALTER TABLE "patients" RENAME COLUMN "Full_Name" TO full_name;
Good discussion here: http://blog.lerner.co.il/quoting-postgresql/

Related

Unexplained massive number of shopping Carts in Rails database in Production

I built an e-commerce app using Rails 5, postgres and heroku, with a classic shopping Cart using the session object to store the cart_id. Thanks to a warning from heroku, I discovered that the numbers of Carts in my database was increasing in a very weird way. It can be more than 50 per minute; and it's not due to many customers visiting the site, which is quite small for the moment. At other moments, the numbers of rows of the Carts table is stable.
Here is my application controller:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
around_action :switch_locale
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :current_cart
after_action :store_action
def switch_locale(&action)
locale = params[:locale] || I18n.default_locale
I18n.with_locale(locale, &action)
end
def default_url_options
{ locale: I18n.locale == I18n.default_locale ? nil : I18n.locale }
end
def current_cart
if session[:cart_id]
cart = Cart.find_by_id(session[:cart_id])
if cart == nil
session[:cart_id] = nil
end
if cart.present?
#current_cart = cart
else
session[:cart_id] = nil
end
end
if session[:cart_id] == nil
#current_cart = Cart.create
session[:cart_id] = #current_cart.id
end
end
def store_action
return unless request.get?
if (request.path != "/users/sign_in" &&
request.path != "/users/sign_up" &&
request.path != "/users/password/new" &&
request.path != "/users/password/edit" &&
request.path != "/users/confirmation" &&
request.path != "/users/sign_out" &&
!request.xhr?) # don't store ajax calls
store_location_for(:user, request.fullpath)
end
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
end
end
cart.rb
class Cart < ApplicationRecord
has_many :order_items, dependent: :destroy
has_many :products, through: :order_items
monetize :amount_cents_cents
end
and the Orders controller:
class OrdersController < ApplicationController
before_action :authenticate_user!
def create
#order = Order.new
total = []
#current_cart.order_items.each do |item|
total << item.product.price_cents * item.quantity.to_i
end
#order.amount_cents_cents = total.sum
if #order.amount_cents_cents == 0
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
redirect_to root_path
else
#current_cart.order_items.each do |item|
#order.order_items << item
item.cart_id = nil
end
#user = current_user
#order.user_id = #user.id
#order.save
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
redirect_to order_path(#order)
end
Here are Heroku logs from yesterday :
2021-03-09T16:25:31.994631+00:00 app[web.1]: D, [2021-03-09T16:25:31.994568 #4] DEBUG -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] Cart Create (1.1ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", "2021-03-09 16:25:31.993019"], ["updated_at", "2021-03-09 16:25:31.993019"]]
2021-03-09T16:25:31.996727+00:00 app[web.1]: D, [2021-03-09T16:25:31.996649 #4] DEBUG -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] (1.8ms) COMMIT
2021-03-09T16:25:31.998717+00:00 app[web.1]: D, [2021-03-09T16:25:31.998636 #4] DEBUG -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] OrderItem Load (1.1ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = $1 [["cart_id", 102580]]
2021-03-09T16:25:31.999401+00:00 app[web.1]: I, [2021-03-09T16:25:31.999343 #4] INFO -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:25:32.000698+00:00 app[web.1]: I, [2021-03-09T16:25:32.000634 #4] INFO -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] Rendered carts/show.html.erb within layouts/application (1.2ms)
2021-03-09T16:25:32.003572+00:00 app[web.1]: I, [2021-03-09T16:25:32.003492 #4] INFO -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] Rendered shared/_navbar.html.erb (2.0ms)
2021-03-09T16:25:32.003861+00:00 app[web.1]: I, [2021-03-09T16:25:32.003787 #4] INFO -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] Rendered shared/_flashes.html.erb (0.1ms)
2021-03-09T16:25:32.004305+00:00 app[web.1]: I, [2021-03-09T16:25:32.004209 #4] INFO -- : [bc4f12a5-b503-435a-bd45-eac3c3adc006] Completed 200 OK in 14ms (Views: 5.0ms | ActiveRecord: 5.0ms)
2021-03-09T16:25:32.006089+00:00 heroku[router]: at=info method=GET path="/en/carts/100155" host=www.bravacoffeeroasters.com request_id=bc4f12a5-b503-435a-bd45-eac3c3adc006 fwd="5.9.108.254" dyno=web.1 connect=0ms service=20ms status=200 bytes=8003 protocol=https
2021-03-09T16:25:39.827045+00:00 app[web.1]: I, [2021-03-09T16:25:39.826944 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Started GET "/en/carts/100158" for 5.9.108.254 at 2021-03-09 16:25:39 +0000
2021-03-09T16:25:39.828020+00:00 app[web.1]: I, [2021-03-09T16:25:39.827942 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Processing by CartsController#show as HTML
2021-03-09T16:25:39.828070+00:00 app[web.1]: I, [2021-03-09T16:25:39.828014 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Parameters: {"locale"=>"en", "id"=>"100158"}
2021-03-09T16:25:39.831671+00:00 app[web.1]: D, [2021-03-09T16:25:39.831585 #4] DEBUG -- : [794133c6-3a8d-4952-b38b-148dd1c78941] (1.2ms) BEGIN
2021-03-09T16:25:39.834022+00:00 app[web.1]: D, [2021-03-09T16:25:39.833931 #4] DEBUG -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Cart Create (1.3ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", "2021-03-09 16:25:39.832057"], ["updated_at", "2021-03-09 16:25:39.832057"]]
2021-03-09T16:25:39.836952+00:00 app[web.1]: D, [2021-03-09T16:25:39.836843 #4] DEBUG -- : [794133c6-3a8d-4952-b38b-148dd1c78941] (2.6ms) COMMIT
2021-03-09T16:25:39.838769+00:00 app[web.1]: D, [2021-03-09T16:25:39.838664 #4] DEBUG -- : [794133c6-3a8d-4952-b38b-148dd1c78941] OrderItem Load (1.1ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = $1 [["cart_id", 102581]]
2021-03-09T16:25:39.839411+00:00 app[web.1]: I, [2021-03-09T16:25:39.839342 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:25:39.840887+00:00 app[web.1]: I, [2021-03-09T16:25:39.840812 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Rendered carts/show.html.erb within layouts/application (1.4ms)
2021-03-09T16:25:39.843830+00:00 app[web.1]: I, [2021-03-09T16:25:39.843767 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Rendered shared/_navbar.html.erb (2.0ms)
2021-03-09T16:25:39.844041+00:00 app[web.1]: I, [2021-03-09T16:25:39.843979 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Rendered shared/_flashes.html.erb (0.1ms)
2021-03-09T16:25:39.844419+00:00 app[web.1]: I, [2021-03-09T16:25:39.844315 #4] INFO -- : [794133c6-3a8d-4952-b38b-148dd1c78941] Completed 200 OK in 16ms (Views: 5.1ms | ActiveRecord: 6.2ms)
2021-03-09T16:25:39.846119+00:00 heroku[router]: at=info method=GET path="/en/carts/100158" host=www.bravacoffeeroasters.com request_id=794133c6-3a8d-4952-b38b-148dd1c78941 fwd="5.9.108.254" dyno=web.1 connect=0ms service=21ms status=200 bytes=8007 protocol=https
2021-03-09T16:25:44.393247+00:00 app[web.1]: I, [2021-03-09T16:25:44.393143 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Started GET "/en/carts/100162" for 5.9.108.254 at 2021-03-09 16:25:44 +0000
2021-03-09T16:25:44.394349+00:00 app[web.1]: I, [2021-03-09T16:25:44.394257 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Processing by CartsController#show as HTML
2021-03-09T16:25:44.394397+00:00 app[web.1]: I, [2021-03-09T16:25:44.394331 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Parameters: {"locale"=>"en", "id"=>"100162"}
2021-03-09T16:25:44.402068+00:00 app[web.1]: D, [2021-03-09T16:25:44.401988 #4] DEBUG -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] (1.0ms) BEGIN
2021-03-09T16:25:44.404434+00:00 app[web.1]: D, [2021-03-09T16:25:44.404308 #4] DEBUG -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Cart Create (1.2ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", "2021-03-09 16:25:44.402575"], ["updated_at", "2021-03-09 16:25:44.402575"]]
2021-03-09T16:25:44.406742+00:00 app[web.1]: D, [2021-03-09T16:25:44.406662 #4] DEBUG -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] (1.9ms) COMMIT
2021-03-09T16:25:44.408565+00:00 app[web.1]: D, [2021-03-09T16:25:44.408479 #4] DEBUG -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] OrderItem Load (1.0ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = $1 [["cart_id", 102582]]
2021-03-09T16:25:44.409183+00:00 app[web.1]: I, [2021-03-09T16:25:44.409106 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:25:44.410524+00:00 app[web.1]: I, [2021-03-09T16:25:44.410452 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Rendered carts/show.html.erb within layouts/application (1.2ms)
2021-03-09T16:25:44.413558+00:00 app[web.1]: I, [2021-03-09T16:25:44.413470 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Rendered shared/_navbar.html.erb (2.0ms)
2021-03-09T16:25:44.413840+00:00 app[web.1]: I, [2021-03-09T16:25:44.413772 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Rendered shared/_flashes.html.erb (0.1ms)
2021-03-09T16:25:44.414160+00:00 app[web.1]: I, [2021-03-09T16:25:44.414098 #4] INFO -- : [55da48a9-0088-4a3d-984a-f58f8a81245a] Completed 200 OK in 20ms (Views: 5.1ms | ActiveRecord: 5.2ms)
2021-03-09T16:25:44.416182+00:00 heroku[router]: at=info method=GET path="/en/carts/100162" host=www.bravacoffeeroasters.com request_id=55da48a9-0088-4a3d-984a-f58f8a81245a fwd="5.9.108.254" dyno=web.1 connect=0ms service=25ms status=200 bytes=8001 protocol=https
2021-03-09T16:25:49.749861+00:00 app[web.1]: I, [2021-03-09T16:25:49.749718 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Started GET "/en/carts/100167" for 5.9.108.254 at 2021-03-09 16:25:49 +0000
2021-03-09T16:25:49.751269+00:00 app[web.1]: I, [2021-03-09T16:25:49.751173 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Processing by CartsController#show as HTML
2021-03-09T16:25:49.751359+00:00 app[web.1]: I, [2021-03-09T16:25:49.751277 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Parameters: {"locale"=>"en", "id"=>"100167"}
2021-03-09T16:25:49.754570+00:00 app[web.1]: D, [2021-03-09T16:25:49.754464 #4] DEBUG -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] (1.1ms) BEGIN
2021-03-09T16:25:49.757801+00:00 app[web.1]: D, [2021-03-09T16:25:49.757693 #4] DEBUG -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Cart Create (1.2ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", "2021-03-09 16:25:49.755581"], ["updated_at", "2021-03-09 16:25:49.755581"]]
2021-03-09T16:25:49.760623+00:00 app[web.1]: D, [2021-03-09T16:25:49.760418 #4] DEBUG -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] (2.2ms) COMMIT
2021-03-09T16:25:49.762818+00:00 app[web.1]: D, [2021-03-09T16:25:49.762617 #4] DEBUG -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] OrderItem Load (1.1ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = $1 [["cart_id", 102583]]
2021-03-09T16:25:49.764416+00:00 app[web.1]: I, [2021-03-09T16:25:49.764303 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:25:49.766505+00:00 app[web.1]: I, [2021-03-09T16:25:49.766423 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Rendered carts/show.html.erb within layouts/application (1.9ms)
2021-03-09T16:25:49.771851+00:00 app[web.1]: I, [2021-03-09T16:25:49.771765 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Rendered shared/_navbar.html.erb (3.8ms)
2021-03-09T16:25:49.772151+00:00 app[web.1]: I, [2021-03-09T16:25:49.772072 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Rendered shared/_flashes.html.erb (0.1ms)
2021-03-09T16:25:49.772669+00:00 app[web.1]: I, [2021-03-09T16:25:49.772589 #4] INFO -- : [c9f138de-a820-4b0b-b87e-ad2f99f6c9f5] Completed 200 OK in 21ms (Views: 8.7ms | ActiveRecord: 5.7ms)
2021-03-09T16:25:49.775098+00:00 heroku[router]: at=info method=GET path="/en/carts/100167" host=www.bravacoffeeroasters.com request_id=c9f138de-a820-4b0b-b87e-ad2f99f6c9f5 fwd="5.9.108.254" dyno=web.1 connect=0ms service=28ms status=200 bytes=8005 protocol=https
2021-03-09T16:25:55.885797+00:00 app[web.1]: I, [2021-03-09T16:25:55.885705 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Started GET "/en/carts/100171" for 5.9.108.254 at 2021-03-09 16:25:55 +0000
2021-03-09T16:25:55.886791+00:00 app[web.1]: I, [2021-03-09T16:25:55.886726 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Processing by CartsController#show as HTML
2021-03-09T16:25:55.886851+00:00 app[web.1]: I, [2021-03-09T16:25:55.886800 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Parameters: {"locale"=>"en", "id"=>"100171"}
2021-03-09T16:25:55.889588+00:00 app[web.1]: D, [2021-03-09T16:25:55.889521 #4] DEBUG -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] (1.0ms) BEGIN
2021-03-09T16:25:55.891685+00:00 app[web.1]: D, [2021-03-09T16:25:55.891611 #4] DEBUG -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Cart Create (1.2ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", "2021-03-09 16:25:55.889991"], ["updated_at", "2021-03-09 16:25:55.889991"]]
2021-03-09T16:25:55.894147+00:00 app[web.1]: D, [2021-03-09T16:25:55.894080 #4] DEBUG -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] (2.1ms) COMMIT
2021-03-09T16:25:55.895883+00:00 app[web.1]: D, [2021-03-09T16:25:55.895820 #4] DEBUG -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] OrderItem Load (1.1ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = $1 [["cart_id", 102584]]
2021-03-09T16:25:55.896595+00:00 app[web.1]: I, [2021-03-09T16:25:55.896512 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:25:55.898042+00:00 app[web.1]: I, [2021-03-09T16:25:55.897982 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Rendered carts/show.html.erb within layouts/application (1.4ms)
2021-03-09T16:25:55.900969+00:00 app[web.1]: I, [2021-03-09T16:25:55.900906 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Rendered shared/_navbar.html.erb (2.0ms)
2021-03-09T16:25:55.901154+00:00 app[web.1]: I, [2021-03-09T16:25:55.901098 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Rendered shared/_flashes.html.erb (0.0ms)
2021-03-09T16:25:55.901439+00:00 app[web.1]: I, [2021-03-09T16:25:55.901385 #4] INFO -- : [e2305a65-52be-432a-867f-ac1c0f459e1b] Completed 200 OK in 14ms (Views: 5.1ms | ActiveRecord: 5.3ms)
2021-03-09T16:25:55.903393+00:00 heroku[router]: at=info method=GET path="/en/carts/100171" host=www.bravacoffeeroasters.com request_id=e2305a65-52be-432a-867f-ac1c0f459e1b fwd="5.9.108.254" dyno=web.1 connect=0ms service=20ms status=200 bytes=8005 protocol=https
2021-03-09T16:26:02.095206+00:00 app[web.1]: I, [2021-03-09T16:26:02.095107 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Started GET "/en/carts/100176" for 5.9.108.254 at 2021-03-09 16:26:02 +0000
2021-03-09T16:26:02.096223+00:00 app[web.1]: I, [2021-03-09T16:26:02.096121 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Processing by CartsController#show as HTML
2021-03-09T16:26:02.096287+00:00 app[web.1]: I, [2021-03-09T16:26:02.096209 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Parameters: {"locale"=>"en", "id"=>"100176"}
2021-03-09T16:26:02.099292+00:00 app[web.1]: D, [2021-03-09T16:26:02.099195 #4] DEBUG -- : [59ebb736-4614-4052-97dd-9517cfd2af70] (1.0ms) BEGIN
2021-03-09T16:26:02.101864+00:00 app[web.1]: D, [2021-03-09T16:26:02.101787 #4] DEBUG -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Cart Create (1.3ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", "2021-03-09 16:26:02.099815"], ["updated_at", "2021-03-09 16:26:02.099815"]]
2021-03-09T16:26:02.104590+00:00 app[web.1]: D, [2021-03-09T16:26:02.104462 #4] DEBUG -- : [59ebb736-4614-4052-97dd-9517cfd2af70] (2.3ms) COMMIT
2021-03-09T16:26:02.106692+00:00 app[web.1]: D, [2021-03-09T16:26:02.106619 #4] DEBUG -- : [59ebb736-4614-4052-97dd-9517cfd2af70] OrderItem Load (1.3ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = $1 [["cart_id", 102585]]
2021-03-09T16:26:02.107212+00:00 app[web.1]: I, [2021-03-09T16:26:02.107150 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:26:02.108495+00:00 app[web.1]: I, [2021-03-09T16:26:02.108422 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Rendered carts/show.html.erb within layouts/application (1.2ms)
2021-03-09T16:26:02.111100+00:00 app[web.1]: I, [2021-03-09T16:26:02.111034 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Rendered shared/_navbar.html.erb (1.7ms)
2021-03-09T16:26:02.111294+00:00 app[web.1]: I, [2021-03-09T16:26:02.111230 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Rendered shared/_flashes.html.erb (0.1ms)
2021-03-09T16:26:02.111695+00:00 app[web.1]: I, [2021-03-09T16:26:02.111563 #4] INFO -- : [59ebb736-4614-4052-97dd-9517cfd2af70] Completed 200 OK in 15ms (Views: 4.5ms | ActiveRecord: 5.9ms)
2021-03-09T16:26:02.114053+00:00 heroku[router]: at=info method=GET path="/en/carts/100176" host=www.bravacoffeeroasters.com request_id=59ebb736-4614-4052-97dd-9517cfd2af70 fwd="5.9.108.254" dyno=web.1 connect=0ms service=21ms status=200 bytes=8011 protocol=https
2021-03-09T16:26:07.168587+00:00 app[web.1]: I, [2021-03-09T16:26:07.168395 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Started GET "/en/carts/100181" for 5.9.108.254 at 2021-03-09 16:26:07 +0000
2021-03-09T16:26:07.169524+00:00 app[web.1]: I, [2021-03-09T16:26:07.169461 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Processing by CartsController#show as HTML
2021-03-09T16:26:07.169585+00:00 app[web.1]: I, [2021-03-09T16:26:07.169533 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Parameters: {"locale"=>"en", "id"=>"100181"}
2021-03-09T16:26:07.172559+00:00 app[web.1]: D, [2021-03-09T16:26:07.172490 #4] DEBUG -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] (1.0ms) BEGIN
2021-03-09T16:26:07.174639+00:00 app[web.1]: D, [2021-03-09T16:26:07.174577 #4] DEBUG -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Cart Create (1.2ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", "2021-03-09 16:26:07.172954"], ["updated_at", "2021-03-09 16:26:07.172954"]]
2021-03-09T16:26:07.176801+00:00 app[web.1]: D, [2021-03-09T16:26:07.176731 #4] DEBUG -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] (1.9ms) COMMIT
2021-03-09T16:26:07.178631+00:00 app[web.1]: D, [2021-03-09T16:26:07.178567 #4] DEBUG -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] OrderItem Load (1.1ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = $1 [["cart_id", 102586]]
2021-03-09T16:26:07.179179+00:00 app[web.1]: I, [2021-03-09T16:26:07.179119 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:26:07.180626+00:00 app[web.1]: I, [2021-03-09T16:26:07.180554 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Rendered carts/show.html.erb within layouts/application (1.3ms)
2021-03-09T16:26:07.183396+00:00 app[web.1]: I, [2021-03-09T16:26:07.183332 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Rendered shared/_navbar.html.erb (1.9ms)
2021-03-09T16:26:07.183592+00:00 app[web.1]: I, [2021-03-09T16:26:07.183524 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Rendered shared/_flashes.html.erb (0.0ms)
2021-03-09T16:26:07.183924+00:00 app[web.1]: I, [2021-03-09T16:26:07.183852 #4] INFO -- : [512ac9cd-da40-435d-a7b8-986ad4209a31] Completed 200 OK in 14ms (Views: 4.8ms | ActiveRecord: 5.1ms)
2021-03-09T16:26:07.186556+00:00 heroku[router]: at=info method=GET path="/en/carts/100181" host=www.bravacoffeeroasters.com request_id=512ac9cd-da40-435d-a7b8-986ad4209a31 fwd="5.9.108.254" dyno=web.1 connect=0ms service=20ms status=200 bytes=8009 protocol=https
2021-03-09T16:26:12.791059+00:00 app[web.1]: I, [2021-03-09T16:26:12.790956 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Started GET "/en/carts/100185" for 5.9.108.254 at 2021-03-09 16:26:12 +0000
2021-03-09T16:26:12.792005+00:00 app[web.1]: I, [2021-03-09T16:26:12.791938 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Processing by CartsController#show as HTML
2021-03-09T16:26:12.792075+00:00 app[web.1]: I, [2021-03-09T16:26:12.792011 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Parameters: {"locale"=>"en", "id"=>"100185"}
2021-03-09T16:26:12.794860+00:00 app[web.1]: D, [2021-03-09T16:26:12.794781 #4] DEBUG -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] (1.0ms) BEGIN
2021-03-09T16:26:12.797433+00:00 app[web.1]: D, [2021-03-09T16:26:12.797358 #4] DEBUG -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Cart Create (1.2ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", "2021-03-09 16:26:12.795289"], ["updated_at", "2021-03-09 16:26:12.795289"]]
2021-03-09T16:26:12.799742+00:00 app[web.1]: D, [2021-03-09T16:26:12.799657 #4] DEBUG -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] (1.8ms) COMMIT
2021-03-09T16:26:12.801691+00:00 app[web.1]: D, [2021-03-09T16:26:12.801618 #4] DEBUG -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] OrderItem Load (1.2ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."cart_id" = $1 [["cart_id", 102587]]
2021-03-09T16:26:12.802222+00:00 app[web.1]: I, [2021-03-09T16:26:12.802153 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Rendering carts/show.html.erb within layouts/application
2021-03-09T16:26:12.803798+00:00 app[web.1]: I, [2021-03-09T16:26:12.803727 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Rendered carts/show.html.erb within layouts/application (1.5ms)
2021-03-09T16:26:12.807236+00:00 app[web.1]: I, [2021-03-09T16:26:12.807070 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Rendered shared/_navbar.html.erb (2.3ms)
2021-03-09T16:26:12.807516+00:00 app[web.1]: I, [2021-03-09T16:26:12.807446 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Rendered shared/_flashes.html.erb (0.1ms)
2021-03-09T16:26:12.807908+00:00 app[web.1]: I, [2021-03-09T16:26:12.807840 #4] INFO -- : [ee78de9f-ffb9-4c3b-a484-8438903fd9c9] Completed 200 OK in 16ms (Views: 5.8ms | ActiveRecord: 5.2ms)
2021-03-09T16:26:12.809202+00:00 heroku[router]: at=info method=GET path="/en/carts/100185" host=www.bravacoffeeroasters.com request_id=ee78de9f-ffb9-4c3b-a484-8438903fd9c9 fwd="5.9.108.254" dyno=web.1 connect=0ms service=20ms status=200 bytes=8013 protocol=https
I deleted all the Carts of the production database yesterday and today I have already more than 3000 rows. This sounds bad to me. Is anyone could help fixing the problem?
If I am reading your code correctly:
before_action :current_cart
def current_cart
...
if session[:cart_id] == nil
#current_cart = Cart.create
session[:cart_id] = #current_cart.id
end
end
A before_action added in ApplicationController will execute for every action in every controller that inherits from ApplicationController.
Even your homepage probably executes it. When session[:cart_id] is nil (which by your current issue, it happens a lot) every access will create a Cart.
The purpose of current_cart, IMO, same as current_user is to find a cart not create one. If it's not present, then it's other action's purpose to create it.
You should revise your logic a little. Good luck!

Error on heroku but on local with Ruby on Rails

I am creating a Blog app. This is working well on local, but on Hroku, when I tried to post comments, We're sorry, but something went wrong. appeared.
On local, I can posts comments on each article.
I use sqLite3 for local, and postgre for heroku.
Error log
2019-03-25T07:15:44.567261+00:00 app[web.1]: D, [2019-03-25T07:15:44.567194 #4] DEBUG -- : [ff69042f-aa16-4d59-8905-8b5beacee242] [1m[36mUser Load (0.8ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-03-25T07:15:44.569317+00:00 app[web.1]: D, [2019-03-25T07:15:44.569236 #4] DEBUG -- : [ff69042f-aa16-4d59-8905-8b5beacee242] [1m[36mArticle Load (1.2ms)[0m [1m[34mSELECT "articles".* FROM "articles" WHERE (2) LIMIT $1[0m [["LIMIT", 1]]
2019-03-25T07:15:44.569537+00:00 app[web.1]: I, [2019-03-25T07:15:44.569468 #4] INFO -- : [ff69042f-aa16-4d59-8905-8b5beacee242] Completed 500 Internal Server Error in 5ms (ActiveRecord: 2.0ms)
2019-03-25T07:15:44.570193+00:00 app[web.1]: F, [2019-03-25T07:15:44.570137 #4] FATAL -- : [ff69042f-aa16-4d59-8905-8b5beacee242]
2019-03-25T07:15:44.570287+00:00 app[web.1]: F, [2019-03-25T07:15:44.570226 #4] FATAL -- : [ff69042f-aa16-4d59-8905-8b5beacee242] ActiveRecord::StatementInvalid (PG::DatatypeMismatch: ERROR: argument of WHERE must be type boolean, not type integer
2019-03-25T07:15:44.570292+00:00 app[web.1]: LINE 1: SELECT "articles".* FROM "articles" WHERE (2) LIMIT $1
2019-03-25T07:15:44.570294+00:00 app[web.1]: ^
2019-03-25T07:15:44.570296+00:00 app[web.1]: : SELECT "articles".* FROM "articles" WHERE (2) LIMIT $1):
2019-03-25T07:15:44.570339+00:00 app[web.1]: F, [2019-03-25T07:15:44.570285 #4] FATAL -- : [ff69042f-aa16-4d59-8905-8b5beacee242]
2019-03-25T07:15:44.570427+00:00 app[web.1]: F, [2019-03-25T07:15:44.570372 #4] FATAL -- : [ff69042f-aa16-4d59-8905-8b5beacee242] app/controllers/comments_controller.rb:7:in `create'
timestamp_create_comments.rb
class CreateComments < ActiveRecord::Migration[5.2]
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :article, foreign_key: true
t.timestamps
end
end
end

NoMethodError (undefined method `connect_timeout=' for #<Typhoeus::Request:0x000000000782ded8>):

I am trying to implement Log-In with Facebook to my app. Everything worked fine in development but for production I had to upgrade the app to https and when I pushed to production, nothing works anymore.
I have no idea how to debug this since it's in production. Any help would be appreciated.
Those are the server logs:
2018-09-17T19:07:21.708757+00:00 app[web.1]: I, [2018-09-17T19:07:21.708637 #4] INFO -- : [c8077522-7674-46e5-97b8-c74456194630] Started GET "/users/auth/facebook" for 213.190.86.42 at 2018-09-17 19:07:21 +0000
2018-09-17T19:07:21.709189+00:00 app[web.1]: I, [2018-09-17T19:07:21.709128 #4] INFO -- omniauth: (facebook) Request phase initiated.
2018-09-17T19:07:22.442651+00:00 app[web.1]: I, [2018-09-17T19:07:22.442540 #4] INFO -- : [3d25cc49-e2ec-4593-bf2f-d06d5595cd78] Started GET "/users/auth/facebook/callback?code=AQDCk0zcgI3_D0ZEuO5l6pWjAbhRgSelpfYcPB_f70hwffFdhs2MRp1rYd1fktORGUhqruLXbB40DXbbbx71zMHD2hH5UyndDQXbWpZ4gIoDuGVqO6IgFMwQuv0Jp_-PVQ2eokb_aW_ZfVY_LkIYPUWqy4fGihmWgd3S4cSlpo9zqV_0m9Ra4avOXQjw5a8VQEq2WNlIsun4J77x4EBzrOh_9xtVG582YV2pZy6tFE83QsvsmNkpBEzmWsV0bnGek94hbnbkpKRmImHfCLD5iyJ3HWZ3uQLG3FypdGZHjo8BtQ23Y2GeD5lnsyGlvqGRX4M&state=f58628a503514d6fd368e47a88f0f1c383e1dbe519b4ad09" for 213.190.86.42 at 2018-09-17 19:07:22 +0000
2018-09-17T19:07:22.443281+00:00 app[web.1]: I, [2018-09-17T19:07:22.443215 #4] INFO -- omniauth: (facebook) Callback phase initiated.
2018-09-17T19:07:22.958420+00:00 app[web.1]: I, [2018-09-17T19:07:22.958291 #4] INFO -- : [3d25cc49-e2ec-4593-bf2f-d06d5595cd78] Processing by Users::OmniauthCallbacksController#facebook as HTML
2018-09-17T19:07:22.963677+00:00 app[web.1]: I, [2018-09-17T19:07:22.962975 #4] INFO -- : [3d25cc49-e2ec-4593-bf2f-d06d5595cd78] Parameters: {"code"=>"AQDCk0zcgI3_D0ZEuO5l6pWjAbhRgSelpfYcPB_f70hwffFdhs2MRp1rYd1fktORGUhqruLXbB40DXbbbx71zMHD2hH5UyndDQXbWpZ4gIoDuGVqO6IgFMwQuv0Jp_-PVQ2eokb_aW_ZfVY_LkIYPUWqy4fGihmWgd3S4cSlpo9zqV_0m9Ra4avOXQjw5a8VQEq2WNlIsun4J77x4EBzrOh_9xtVG582YV2pZy6tFE83QsvsmNkpBEzmWsV0bnGek94hbnbkpKRmImHfCLD5iyJ3HWZ3uQLG3FypdGZHjo8BtQ23Y2GeD5lnsyGlvqGRX4M", "state"=>"f58628a503514d6fd368e47a88f0f1c383e1dbe519b4ad09"}
2018-09-17T19:07:22.987503+00:00 app[web.1]: D, [2018-09-17T19:07:22.987389 #4] DEBUG -- : [3d25cc49-e2ec-4593-bf2f-d06d5595cd78] User Load (3.9ms) SELECT "users".* FROM "users" WHERE "users"."provider" = $1 AND "users"."uid" = $2 LIMIT $3 [["provider", "facebook"], ["uid", "10156065770667984"], ["LIMIT", 1]]
2018-09-17T19:07:22.990121+00:00 app[web.1]: D, [2018-09-17T19:07:22.990015 #4] DEBUG -- : [3d25cc49-e2ec-4593-bf2f-d06d5595cd78] (1.6ms) BEGIN
2018-09-17T19:07:22.997442+00:00 app[web.1]: D, [2018-09-17T19:07:22.997336 #4] DEBUG -- : [3d25cc49-e2ec-4593-bf2f-d06d5595cd78] User Exists (2.1ms) SELECT 1 AS one FROM "users" WHERE "users"."username" = $1 AND "users"."id" != $2 LIMIT $3 [["username", "Listy"], ["id", 65], ["LIMIT", 1]]
2018-09-17T19:07:23.002441+00:00 app[web.1]: D, [2018-09-17T19:07:23.002365 #4] DEBUG -- : [3d25cc49-e2ec-4593-bf2f-d06d5595cd78] User Update (2.9ms) UPDATE "users" SET "token" = $1, "token_expiry" = $2, "updated_at" = $3 WHERE "users"."id" = $4 [["token", "EAADxC9YDD2sBAKeTKx0Cgp86Dnm5l774a2vVsZBYwOTnl7TV9egMgBYjTtgY9lFHV6eW90V1KgRspbcHM3mKL92hKPO0fZAVbkNaWjdHzJ8TQiaRYJqZC3NnhNu6GOOr6eFtteA48XWhYXQslchLmFgUky1G50ZD"], ["token_expiry", "2018-11-16 19:07:22"], ["updated_at", "2018-09-17 19:07:22.997902"], ["id", 65]]
2018-09-17T19:07:23.055468+00:00 app[web.1]: D, [2018-09-17T19:07:23.055339 #4] DEBUG -- : [3d25cc49-e2ec-4593-bf2f-d06d5595cd78] (6.5ms) COMMIT
2018-09-17T19:07:23.063263+00:00 app[web.1]: I, [2018-09-17T19:07:23.063174 #4] INFO -- : [3d25cc49-e2ec-4593-bf2f-d06d5595cd78] Completed 500 Internal Server Error in 99ms (Searchkick: 6.4ms | ActiveRecord: 19.1ms)
2018-09-17T19:07:23.064786+00:00 app[web.1]: F, [2018-09-17T19:07:23.064718 #4] FATAL -- : [3d25cc49-e2ec-4593-bf2f-d06d5595cd78]
2018-09-17T19:07:23.062638+00:00 app[web.1]: D, [2018-09-17T19:07:23.062529 #4] DEBUG -- : [3d25cc49-e2ec-4593-bf2f-d06d5595cd78] User Store (6.4ms) {"id":65,"exception":["NoMethodError","undefined method `connect_timeout=' for #\u003cTyphoeus::Request:0x000000000782ded8\u003e"],"exception_object":"undefined method `connect_timeout=' for #\u003cTyphoeus::Request:0x000000000782ded8\u003e"}
2018-09-17T19:07:23.065029+00:00 app[web.1]: F, [2018-09-17T19:07:23.064965 #4] FATAL -- : [3d25cc49-e2ec-4593-bf2f-d06d5595cd78] NoMethodError (undefined method `connect_timeout=' for #<Typhoeus::Request:0x000000000782ded8>):
2018-09-17T19:07:23.065153+00:00 app[web.1]: F, [2018-09-17T19:07:23.065087 #4] FATAL -- : [3d25cc49-e2ec-4593-bf2f-d06d5595cd78]
2018-09-17T19:07:23.065246+00:00 app[web.1]: [3d25cc49-e2ec-4593-bf2f-d06d5595cd78] app/controllers/users/omniauth_callbacks_controller.rb:3:in `facebook'
2018-09-17T19:07:23.065243+00:00 app[web.1]: F, [2018-09-17T19:07:23.065187 #4] FATAL -- : [3d25cc49-e2ec-4593-bf2f-d06d5595cd78] app/models/user.rb:76:in `find_for_facebook_oauth'
2018-09-17T19:07:23.067245+00:00 heroku[router]: at=info method=GET path= "/users/auth/facebook/callback?code= AQDCk0zcgI3_D0ZEuO5l6pWjAbhRgSelpfYcPB_f70hwffFdhs2MRp1rYd1fktORGUhqruLXbB40DXbbbx71zMHD2hH5UyndDQXbWpZ4gIoDuGVqO6IgFMwQuv0Jp_-PVQ2eokb_aW_ZfVY_LkIYPUWqy4fGihmWgd3S4cSlpo9zqV_0m9Ra4avOXQjw5a8VQEq2WNlIsun4J77x4EBzrOh_9xtVG582YV2pZy6tFE83QsvsmNkpBEzmWsV0bnGek94hbnbkpKRmImHfCLD5iyJ3HWZ3uQLG3FypdGZHjo8BtQ23Y2GeD5lnsyGlvqGRX4M&state=f58628a503514d6fd368e47a88f0f1c383e1dbe519b4ad09" host=www.listy.club request_id=3d25cc49-e2ec-4593-bf2f-d06d5595cd78 fwd="213.190.86.42" dyno=web.1 connect=0ms service=630ms status=500 bytes=1891 protocol=https
2018-09-17T19:08:50.249219+00:00 heroku[run.8341]: Process exited with status 0

Rails works fine but not in heroku

My rails app is working fine when I'm running it locally in Ubuntu, but once I deployed it to herokuapp, it causes an error.
forw = Forward.select(:user_id).where(:doc_id => params[:id])
#users = User.where.not(:id => "#{#user.id}").where.not(:id => forw)
#sent = User.where.not(:id => "#{#user.id}").where(:id => forw)
# line 41 is below ↓
#status = Forward.select(:status).where(user_id: #sent.ids).where(doc_id: params[:id])
I think this is where the error is? this is a query in one of my controllers. Any help will be appreciated.
This is the heroku log when I execute the herokuapp.
2018-07-12T10:06:51.973236+00:00 app[web.1]: I, [2018-07-12T10:06:51.973159 #4] INFO -- : [fb2eda7a-41f6-49a8-9383-29902a059f61] Completed 500 Internal Server Error in 8ms (ActiveRecord: 2.7ms)
2018-07-12T10:06:51.974312+00:00 app[web.1]: F, [2018-07-12T10:06:51.974236 #4] FATAL -- : [fb2eda7a-41f6-49a8-9383-29902a059f61]
2018-07-12T10:06:51.974419+00:00 app[web.1]: F, [2018-07-12T10:06:51.974348 #4] FATAL -- : [fb2eda7a-41f6-49a8-9383-29902a059f61] ActiveRecord::StatementInvalid (PG::UndefinedFunction: ERROR: operator does not exist: bigint = text
2018-07-12T10:06:51.974422+00:00 app[web.1]: LINE 1: ...sers" WHERE ("users"."id" != $1) AND "users"."id" IN (SELECT...
2018-07-12T10:06:51.974424+00:00 app[web.1]: ^
2018-07-12T10:06:51.974426+00:00 app[web.1]: HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
2018-07-12T10:06:51.974430+00:00 app[web.1]: : SELECT "users"."id" FROM "users" WHERE ("users"."id" != $1) AND "users"."id" IN (SELECT "forwards"."user_id" FROM "forwards" WHERE "forwards"."doc_id" = $2)):
2018-07-12T10:06:51.974525+00:00 app[web.1]: F, [2018-07-12T10:06:51.974459 #4] FATAL -- : [fb2eda7a-41f6-49a8-9383-29902a059f61]
2018-07-12T10:06:51.974631+00:00 app[web.1]: F, [2018-07-12T10:06:51.974567 #4] FATAL -- : [fb2eda7a-41f6-49a8-9383-29902a059f61] app/controllers/events_controller.rb:41:in `forward'
My gemfile in declaring the database
group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'pg'
end

What am I missing for authenticating a user with Devise and JWT in Rails 5?

I followed this tutorial to set up authentication with JWT and devise https://www.sitepoint.com/introduction-to-using-jwt-in-rails/. My authentication_controller looks like this:
class AuthenticationController < ApplicationController
def authenticate_user
user = User.find_for_database_authentication(email: params[:email])
if user.valid_password?(params[:password])
render json: payload(user)
else
render json: {errors: ['Invalid Username/Password']}, status:
:unauthorized
end
end
private
def payload(user)
return nil unless user && user.id
{
auth_token: JsonWebToken.encode({user_id: user.id}),
user: {id: user.id, email: user.email}
}
end
end
It maps to a route called auth_user, as shown here in my routes.rb file:
Rails.application.routes.draw do
resources :reviews
resources :people do
resources :reviews
end
post 'auth_user' => 'authentication#authenticate_user'
devise_for :users, :controllers => {sessions: 'sessions', registrations:
'registrations'}
# For details on the DSL available within this file, see
http://guides.rubyonrails.org/routing.html
end
All my other routes work, including posting to the devise routes, but this one give a 500 internal server error. Any ideas of what could be causing this behavior are greatly appreciated! For what its worth, I'm on Rails 5.1.5, JWT 2.1.0, and Devise 4.4.3. Thanks!
I checked the heroku logs after trying to send a request. They look like this:
2018-04-26T20:53:32.801349+00:00 app[web.1]: I, [2018-04-26T20:53:32.801222 #4] INFO -- : [bb58b729-4d79-4940-b73d-2bc433c8d224] Started POST "/auth_user" for 199.116.73.196 at 2018-04-26 20:53:32 +0000
2018-04-26T20:53:32.824053+00:00 app[web.1]: I, [2018-04-26T20:53:32.823923 #4] INFO -- : [bb58b729-4d79-4940-b73d-2bc433c8d224] Processing by AuthenticationController#authenticate_user as */*
2018-04-26T20:53:32.824188+00:00 app[web.1]: I, [2018-04-26T20:53:32.824119 #4] INFO -- : [bb58b729-4d79-4940-b73d-2bc433c8d224] Parameters: {"email"=>"\"mike#mike.com\"", "password"=>"[FILTERED]"}
2018-04-26T20:53:32.953483+00:00 app[web.1]: D, [2018-04-26T20:53:32.953329 #4] DEBUG -- : [bb58b729-4d79-4940-b73d-2bc433c8d224] [1m[36mUser Load (8.4ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."email" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m [["email", "\"mike#mike.com\""], ["LIMIT", 1]]
2018-04-26T20:53:32.963709+00:00 app[web.1]: I, [2018-04-26T20:53:32.963569 #4] INFO -- : [bb58b729-4d79-4940-b73d-2bc433c8d224] Completed 500 Internal Server Error in 139ms (ActiveRecord: 53.2ms)
2018-04-26T20:53:32.964567+00:00 app[web.1]: F, [2018-04-26T20:53:32.964492 #4] FATAL -- : [bb58b729-4d79-4940-b73d-2bc433c8d224]
2018-04-26T20:53:32.964715+00:00 app[web.1]: F, [2018-04-26T20:53:32.964649 #4] FATAL -- : [bb58b729-4d79-4940-b73d-2bc433c8d224] NoMethodError (undefined method `valid_password?' for nil:NilClass):
2018-04-26T20:53:32.964805+00:00 app[web.1]: F, [2018-04-26T20:53:32.964742 #4] FATAL -- : [bb58b729-4d79-4940-b73d-2bc433c8d224]
2018-04-26T20:53:32.964904+00:00 app[web.1]: F, [2018-04-26T20:53:32.964844 #4] FATAL -- : [bb58b729-4d79-4940-b73d-2bc433c8d224] app/controllers/authentication_controller.rb:4:in `authenticate_user'
2018-04-26T20:53:32.966716+00:00 heroku[router]: at=info method=POST path="/auth_user" host=rateyouracquaintanceapi.herokuapp.com request_id=bb58b729-4d79-4940-b73d-2bc433c8d224 fwd="199.116.73.196" dyno=web.1 connect=1ms service=172ms status=500 bytes=203 protocol=https
Still a bit baffled as to what's breaking, but at least it's clear now where the problem is!
Change the code in authenticate_user to this:
def authenticate_user
user = User.find_for_database_authentication(email: params[:email])
if user && user.valid_password?(params[:password])
render json: payload(user)
else
render json: {errors: ['Invalid Username/Password']}, status:
:unauthorized
end
end
Now I get the expected json response for if the user was not found, but if I input the data for a valid user it does not give the proper response. Could this be a problem with the find_for_database_authentication method? The logs now show this:
2018-04-26T21:24:07.327940+00:00 heroku[router]: at=info method=POST
path="/auth_user" host=rateyouracquaintanceapi.herokuapp.com
request_id=95dcfdd7-55d5-4a24-b8e4-d597737c4b02 fwd="199.116.73.196"
dyno=web.1 connect=1ms service=15ms status=401 bytes=286 protocol=https
2018-04-26T21:24:07.319101+00:00 app[web.1]: I, [2018-04-26T21:24:07.318981
#4] INFO -- : [95dcfdd7-55d5-4a24-b8e4-d597737c4b02] Started POST
"/auth_user" for 199.116.73.196 at 2018-04-26 21:24:07 +0000
2018-04-26T21:24:07.320412+00:00 app[web.1]: I, [2018-04-26T21:24:07.320296
#4] INFO -- : [95dcfdd7-55d5-4a24-b8e4-d597737c4b02] Processing by
AuthenticationController#authenticate_user as */*
2018-04-26T21:24:07.320476+00:00 app[web.1]: I, [2018-04-26T21:24:07.320410
#4] INFO -- : [95dcfdd7-55d5-4a24-b8e4-d597737c4b02] Parameters:
{"email"=>"\"mike#mike.com\"", "password"=>"[FILTERED]"}
2018-04-26T21:24:07.329226+00:00 app[web.1]: D, [2018-04-26T21:24:07.328689
#4] DEBUG -- : [95dcfdd7-55d5-4a24-b8e4-d597737c4b02] [1m[36mUser Load
(5.1ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."email"
=
$1 LIMIT $2[0m [["email", "\"mike#mike.com\""], ["LIMIT", 1]]
2018-04-26T21:24:07.330108+00:00 app[web.1]: I, [2018-04-26T21:24:07.330005
#4] INFO -- : [95dcfdd7-55d5-4a24-b8e4-d597737c4b02]
[active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash
(0.17ms)
2018-04-26T21:24:07.330482+00:00 app[web.1]: I, [2018-04-26T21:24:07.330370
#4] INFO -- : [95dcfdd7-55d5-4a24-b8e4-d597737c4b02] Completed 401
Unauthorized in 10ms (Views: 1.0ms | ActiveRecord: 5.1ms)
Looking at your logs, I see:
NoMethodError (undefined method `valid_password?' for nil:NilClass):
This probably coming from the line:
user.valid_password?(params[:password])
Which means your user variable is nil. This is happening because user = User.find_for_database_authentication(email: params[:email]) is returning nil.
Check to see if your user exists in the database by running the rails console, using heroku run rails c. If not, create it, and your code should work fine.

Resources