Rails works fine but not in heroku - ruby-on-rails

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

Related

Connecting Postgresql to Rails in Heroku

So I recently deployed my Rails backend server to Heroku.
After a little struggling with passing from SQLite to PostgreSQL, I finally got everything running.
Now the problem is, I did rake db:create and rake db:migrate and that went without any errors but now when I make a REST call to the server I have this error (complete) :
2019-07-31T19:42:16.534355+00:00 app[web.1]: I, [2019-07-31T19:42:16.534259 #4] INFO -- : [047c870d-8791-4ec7-90df-a7998b8b17c5] Started POST "/sessions" for 91.176.204.35 at 2019-07-31 19:42:16 +0000
2019-07-31T19:42:16.535824+00:00 app[web.1]: I, [2019-07-31T19:42:16.535063 #4] INFO -- : [047c870d-8791-4ec7-90df-a7998b8b17c5] Processing by SessionsController#create as */*
2019-07-31T19:42:16.535947+00:00 app[web.1]: I, [2019-07-31T19:42:16.535882 #4] INFO -- : [047c870d-8791-4ec7-90df-a7998b8b17c5] Parameters: {"email"=>"titivermeesch#gmail.com", "password"=>"[FILTERED]"}
2019-07-31T19:42:16.566455+00:00 app[web.1]: I, [2019-07-31T19:42:16.566340 #4] INFO -- : [047c870d-8791-4ec7-90df-a7998b8b17c5] Completed 500 Internal Server Error in 30ms (ActiveRecord: 11.2ms)
2019-07-31T19:42:16.567845+00:00 app[web.1]: F, [2019-07-31T19:42:16.567772 #4] FATAL -- : [047c870d-8791-4ec7-90df-a7998b8b17c5]
2019-07-31T19:42:16.567954+00:00 app[web.1]: F, [2019-07-31T19:42:16.567879 #4] FATAL -- : [047c870d-8791-4ec7-90df-a7998b8b17c5] ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "users" does not exist
2019-07-31T19:42:16.567957+00:00 app[web.1]: LINE 8: WHERE a.attrelid = '"users"'::regclass
2019-07-31T19:42:16.567959+00:00 app[web.1]: ^
2019-07-31T19:42:16.567961+00:00 app[web.1]: : SELECT a.attname, format_type(a.atttypid, a.atttypmod),
2019-07-31T19:42:16.567962+00:00 app[web.1]: pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
2019-07-31T19:42:16.567964+00:00 app[web.1]: c.collname, col_description(a.attrelid, a.attnum) AS comment
2019-07-31T19:42:16.567965+00:00 app[web.1]: FROM pg_attribute a
2019-07-31T19:42:16.567966+00:00 app[web.1]: LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
2019-07-31T19:42:16.567968+00:00 app[web.1]: LEFT JOIN pg_type t ON a.atttypid = t.oid
2019-07-31T19:42:16.567969+00:00 app[web.1]: LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
2019-07-31T19:42:16.567971+00:00 app[web.1]: WHERE a.attrelid = '"users"'::regclass
2019-07-31T19:42:16.567972+00:00 app[web.1]: AND a.attnum > 0 AND NOT a.attisdropped
2019-07-31T19:42:16.567973+00:00 app[web.1]: ORDER BY a.attnum
2019-07-31T19:42:16.567975+00:00 app[web.1]: ):
2019-07-31T19:42:16.568064+00:00 app[web.1]: F, [2019-07-31T19:42:16.568002 #4] FATAL -- : [047c870d-8791-4ec7-90df-a7998b8b17c5]
2019-07-31T19:42:16.568171+00:00 app[web.1]: F, [2019-07-31T19:42:16.568104 #4] FATAL -- : [047c870d-8791-4ec7-90df-a7998b8b17c5] app/controllers/sessions_controller.rb:5:in `create'
I have a PostgreSQL add-on installed and it is running. (Otherwise the Rails server wouldn't even boot).
So why do I get this kind of errors on each request? I checked in console and if I do something like User.all I get this :
ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "users" does not exist)
LINE 1: SELECT "users".* FROM "users" LIMIT $1
^
: SELECT "users".* FROM "users" LIMIT $1
This gets me so confused, what did I do wrong?
It sounds like you may have run rake db:migrate locally and not on Heroku. As a result, when you use the Heroku CLI, it doesn't recognize the users table because it doesn't yet exist.
I believe if you use have the pg gem in your Gemfile, Heroku's system will automatically setup a database for you. It's been a while since I've created a Heroku instance so don't quote me on that.
If you're unsure if you have PostgreSQL with your app, check the app's service add-ons area. Heroku also has more documentation on using PostgreSQL on their platform. It may be a good idea for you to reference their documentation for getting started with Rails as well.
Once you have your PostgreSQL database setup to work with your app, you use the Heroku CLI to migrate the database and create your users table:
heroku rake db:migrate

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

Getting an error in postgresql where column name cannot be found?

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/

Form works in development but not production - Heroku

I have a voting platform that I'm building for my company and I am very close to finishing it but the actual voting is failing while in production. I've done all my migrating and everything, and it's still working in development.
Here are the logs I'm receiving from heroku:
2019-01-11T18:33:59.235681+00:00 app[web.1]: I,
[2019-01-11T18:33:59.235579 #4] INFO -- :
[772bc37c-00ee-43da-aa81-564d353553bb] Started POST
"/ballots/13/votes" for 75.150.214.2 at 2019-01-11 18:33:59 +0000
2019-01-11T18:33:59.236373+00:00 app[web.1]: I,
[2019-01-11T18:33:59.236296 #4] INFO -- :
[772bc37c-00ee-43da-aa81-564d353553bb] Processing by
VotesController#create as HTML 2019-01-11T18:33:59.236437+00:00
app[web.1]: I, [2019-01-11T18:33:59.236378 #4] INFO -- :
[772bc37c-00ee-43da-aa81-564d353553bb] Parameters: {"utf8"=>"✓",
"authenticity_token"=>"Y/vAh2kYPskozS2yQn3Ky1fOq41abLUjlxho0Q+zXHSndVlaSfwYz7bdoqvintdBXrYNDhsbIduYICpUAx3/0g==",
"user_vote"=>"Why", "vote"=>{"comment"=>""}, "commit"=>"Cast Vote",
"ballot_id"=>"13"} 2019-01-11T18:33:59.239540+00:00 app[web.1]: D,
[2019-01-11T18:33:59.239464 #4] DEBUG -- :
[772bc37c-00ee-43da-aa81-564d353553bb] Ballot Load (0.7ms) SELECT
"ballots".* FROM "ballots" WHERE "ballots"."id" = $1 LIMIT $2 [["id",
13], ["LIMIT", 1]] 2019-01-11T18:33:59.240333+00:00 app[web.1]: I,
[2019-01-11T18:33:59.240244 #4] INFO -- :
[772bc37c-00ee-43da-aa81-564d353553bb] Completed 500 Internal Server
Error in 4ms (ActiveRecord: 0.7ms) 2019-01-11T18:33:59.240846+00:00
app[web.1]: F, [2019-01-11T18:33:59.240770 #4] FATAL -- :
[772bc37c-00ee-43da-aa81-564d353553bb]
2019-01-11T18:33:59.240917+00:00 app[web.1]: F,
[2019-01-11T18:33:59.240847 #4] FATAL -- :
[772bc37c-00ee-43da-aa81-564d353553bb]
ActiveModel::UnknownAttributeError (unknown attribute 'comment' for
Vote.):' '2019-01-11T18:33:59.240981+00:00 app[web.1]: F,
[2019-01-11T18:33:59.240914 #4] FATAL -- :
[772bc37c-00ee-43da-aa81-564d353553bb]
2019-01-11T18:33:59.241051+00:00 app[web.1]: F,
[2019-01-11T18:33:59.240987 #4] FATAL -- :
[772bc37c-00ee-43da-aa81-564d353553bb]
app/controllers/votes_controller.rb:10:in `create'
2019-01-11T18:34:07.533587+00:00 heroku[run.2070]: State changed from
up to complete bobby#bobby-Al
Here's my code for vote create
def create
#ballot = Ballot.find(params[:ballot_id])
#vote = #ballot.votes.new(user_vote: params[:user_vote], comment: params[:vote][:comment])
#vote.user_id = current_user.id
if !current_user.votes.find_by(ballot_id: params[:ballot_id])
#vote.save
redirect_to ballot_path(params[:ballot_id])
else
flash[:alert] = "You've already voted on this ballot"
redirect_to ballot_path(params[:ballot_id])
end
end
It seems like a migration problem, just make sure to run your migrations for the environment production on heroku like this:
heroku run rake db:migrate
Restart your app
heroku restart --app my_app_name
Important
If you generate a migration that creates a table and make a push to heroku, to add another column to that table you shouldn't modify that one, instead you should create another one for that.
When you run rake db:migrate, rails register that on the table schema_migrations, so if you modify a migrate that is already registered is not going to be executed again. This could be the case, check directly on your database and see if the column is there.

Why is this object undefined? Ruby, Rails, Gmail error

I can't seem to figure out why I am getting the error :
2017-01-03T02:57:35.239505+00:00 app[web.1]: I, [2017-01-03T02:57:35.239437 #4] INFO -- : [89b504ee-5835-4bfa-a3ff-91a3a84549f6] Parameters: {"id"=>"3"}
2017-01-03T02:57:35.243975+00:00 app[web.1]: D, [2017-01-03T02:57:35.243890 #4] DEBUG -- : [89b504ee-5835-4bfa-a3ff-91a3a84549f6] User Load (1.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 22], ["LIMIT", 1]]
2017-01-03T02:57:35.468562+00:00 app[web.1]: true
2017-01-03T02:57:35.476648+00:00 app[web.1]: D, [2017-01-03T02:57:35.476563 #4] DEBUG -- : [89b504ee-5835-4bfa-a3ff-91a3a84549f6] Blueprint Load (2.7ms) SELECT "blueprints".* FROM "blueprints" WHERE "blueprints"."id" = $1 LIMIT $2 [["id", 3], ["LIMIT", 1]]
2017-01-03T02:57:35.477654+00:00 app[web.1]: I, [2017-01-03T02:57:35.477582 #4] INFO -- : [89b504ee-5835-4bfa-a3ff-91a3a84549f6] Completed 500 Internal Server Error in 238ms (ActiveRecord: 5.5ms)
2017-01-03T02:57:35.478257+00:00 app[web.1]: F, [2017-01-03T02:57:35.478177 #4] FATAL -- : [89b504ee-5835-4bfa-a3ff-91a3a84549f6]
2017-01-03T02:57:35.478313+00:00 app[web.1]: F, [2017-01-03T02:57:35.478256 #4] FATAL -- : [89b504ee-5835-4bfa-a3ff-91a3a84549f6] Gmail::Client::DeliveryError (Couldn't deliver email: undefined method `subject' for nil:NilClass):
For some reason the object #current_blueprint doesn't seem to be working inside of the gmail.deliver block. Am I missing something obvious?
Appreciate any help. Thanks!
#current_blueprint = Blueprint.find(params[:id])
mail_status = gmail.deliver! do
to "email#gmail.com"
subject #current_blueprint.subject
html_part do
content_type 'text/html; charset=UTF-8'
body #current_blueprint.body
end
end
puts mail_status
Try just current_blueprint without the # sign.
Sometime a library executes a do end with a different self and can thus not access instance variables, but it should be able to access local variables that are in scope.
Just guessing, let me know whether it works.

Resources