So i have this code in my mode
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string
# surname :string
# user_id :integer
# count :integer default(0)
# was :boolean default(FALSE)
# qrcode :string
# created_at :datetime not null
# updated_at :datetime not null
#
class User < ApplicationRecord
validates :name, presence: true
validates :surname, presence: true
validates :user_id, presence: true
after_create :generate_qr
private
def generate_qr
ian = User.find(id)
ian.update_attribute(:qrcode, "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" + user_id.to_s)
# update_attribute(:qrcode, "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" + user_id.to_s)
end
end
Its work fine and generate qr code on my local host. But when i deploy on heroku i have this errors in heroku logs.
2018-06-30T14:23:37.200657+00:00 app[web.1]: I, [2018-06-30T14:23:37.200507 #4] INFO -- : [56b953ac-5a43-4b25-980d-f2bc5c53883a] Parameters: {"utf8"=>"✓", "authenticity_token"=>"mBkhE7alyyKGtS4v+tnW2Vpg6c1z0kwQfbnftqTWdSukTpre29vkqVmeuVoGR7NJzs0EoxiQv0rS+WzJ4hUSOw==", "user"=>{"name"=>"fdsfds", "surname"=>"fdsfdsfds", "user_id"=>"53454"}, "commit"=>"Create User"}
2018-06-30T14:23:37.204598+00:00 app[web.1]: D, [2018-06-30T14:23:37.204527 #4] DEBUG -- : [56b953ac-5a43-4b25-980d-f2bc5c53883a] (1.1ms) BEGIN
2018-06-30T14:23:37.207575+00:00 app[web.1]: D, [2018-06-30T14:23:37.207496 #4] DEBUG -- : [56b953ac-5a43-4b25-980d-f2bc5c53883a] User Create (1.5ms) INSERT INTO "users" ("name", "surname", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "fdsfds"], ["surname", "fdsfdsfds"], ["user_id", 53454], ["created_at", "2018-06-30 14:23:37.204956"], ["updated_at", "2018-06-30 14:23:37.204956"]]
2018-06-30T14:23:37.214137+00:00 app[web.1]: D, [2018-06-30T14:23:37.214060 #4] DEBUG -- : [56b953ac-5a43-4b25-980d-f2bc5c53883a] User Load (1.3ms)
SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 7], ["LIMIT", 1]]
2018-06-30T14:23:37.215729+00:00 app[web.1]: D, [2018-06-30T14:23:37.215664 #4] DEBUG -- : [56b953ac-5a43-4b25-980d-f2bc5c53883a] (1.1ms) ROLLBACK
2018-06-30T14:23:37.224955+00:00 app[web.1]: I, [2018-06-30T14:23:37.224881 #4] INFO -- : [56b953ac-5a43-4b25-980d-f2bc5c53883a] Completed 500 Internal Server Error in 24ms (ActiveRecord: 6.1ms)
2018-06-30T14:23:37.225904+00:00 app[web.1]: F, [2018-06-30T14:23:37.225838 #4] FATAL -- : [56b953ac-5a43-4b25-980d-f2bc5c53883a]
2018-06-30T14:23:37.226043+00:00 app[web.1]: F, [2018-06-30T14:23:37.225990 #4] FATAL -- : [56b953ac-5a43-4b25-980d-f2bc5c53883a] NoMethodError (undefined method `qrcode=' for #<User:0x00000004fd1d68>):
2018-06-30T14:23:37.226091+00:00 app[web.1]: F, [2018-06-30T14:23:37.226046 #4] FATAL -- : [56b953ac-5a43-4b25-980d-f2bc5c53883a]
2018-06-30T14:23:37.226146+00:00 app[web.1]: F, [2018-06-30T14:23:37.226103 #4] FATAL -- : [56b953ac-5a43-4b25-980d-f2bc5c53883a] app/models/user.rb:25:in `generate_qr'
Someone please help. I dont know where is error. Its work fine on my localhost.
Related
I am trying to find out why my update method is not working in my Rails API. It should update the bio field. I have my API hosted on Heroku and am using the Heroku logs to debug in production. I used the exists? method to make sure the user is in the db and yet when the update method is called it rollsback after doing this check. I don't understand what is the cause of this?
Here are the Heroku logs of the output
2022-04-15T02:54:34.083586+00:00 app[web.1]: I, [2022-04-15T02:54:34.083515 #4] INFO -- : [9e5ea776-5b15-420f-8cdc-0601480e0e3d] Started PATCH "/users/8" for 98.248.0.125 at 2022-04-15 02:54:34 +0000
2022-04-15T02:54:34.084345+00:00 app[web.1]: I, [2022-04-15T02:54:34.084290 #4] INFO -- : [9e5ea776-5b15-420f-8cdc-0601480e0e3d] Processing by UsersController#update as HTML
2022-04-15T02:54:34.084376+00:00 app[web.1]: I, [2022-04-15T02:54:34.084350 #4] INFO -- : [9e5ea776-5b15-420f-8cdc-0601480e0e3d] Parameters: {"bio"=>"test", "id"=>"8", "user"=>{"bio"=>"test"}}
2022-04-15T02:54:34.087450+00:00 app[web.1]: D, [2022-04-15T02:54:34.087403 #4] DEBUG -- : [9e5ea776-5b15-420f-8cdc-0601480e0e3d] User Load (1.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 8], ["LIMIT", 1]]
2022-04-15T02:54:34.089711+00:00 app[web.1]: D, [2022-04-15T02:54:34.089664 #4] DEBUG -- : [9e5ea776-5b15-420f-8cdc-0601480e0e3d] User Exists? (1.2ms) SELECT 1 AS one FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 8], ["LIMIT", 1]]
2022-04-15T02:54:34.092004+00:00 app[web.1]: D, [2022-04-15T02:54:34.091963 #4] DEBUG -- : [9e5ea776-5b15-420f-8cdc-0601480e0e3d] TRANSACTION (0.9ms) BEGIN
2022-04-15T02:54:34.093523+00:00 app[web.1]: D, [2022-04-15T02:54:34.093465 #4] DEBUG -- : [9e5ea776-5b15-420f-8cdc-0601480e0e3d] User Exists? (1.4ms) SELECT 1 AS one FROM "users" WHERE "users"."username" = $1 AND "users"."id" != $2 LIMIT $3 [["username", "newperson"], ["id", 8], ["LIMIT", 1]]
2022-04-15T02:54:34.095530+00:00 app[web.1]: D, [2022-04-15T02:54:34.095493 #4] DEBUG -- : [9e5ea776-5b15-420f-8cdc-0601480e0e3d] TRANSACTION (0.9ms) ROLLBACK
2022-04-15T02:54:34.096881+00:00 app[web.1]: I, [2022-04-15T02:54:34.096842 #4] INFO -- : [9e5ea776-5b15-420f-8cdc-0601480e0e3d] [active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash (0.07ms)
2022-04-15T02:54:34.097078+00:00 app[web.1]: I, [2022-04-15T02:54:34.097050 #4] INFO -- : [9e5ea776-5b15-420f-8cdc-0601480e0e3d] Completed 422 Unprocessable Entity in 13ms (Views: 0.6ms | ActiveRecord: 5.5ms | Allocations: 2816)
2022-04-15T02:54:34.101664+00:00 heroku[router]: at=info method=PATCH path="/users/8" host=anime-axis-api.herokuapp.com request_id=9e5ea776-5b15-420f-8cdc-0601480e0e3d fwd="98.248.0.125" dyno=web.1 connect=0ms service=17ms status=422 bytes=1096 protocol=https
Here is my update method:
def update
if User.exists?(8)
#current_user.update!(user_params)
render json: #current_user, status: :ok
end
end
private
def user_params
# added require
params.require(:user).permit(:username, :password, :password_confirmation, :bio, :avatar, :email)
end
My User model:
class User < ApplicationRecord
has_secure_password
has_many :anime_lists
has_many :animes, through: :anime_lists
has_many :manga_lists
has_many :mangas, through: :manga_lists
validates :username, presence: true, confirmation: {case_sensitive: false}, uniqueness: true, length: {in: 6..30}
validates :password, presence: true, confirmation: true
end
I think your validations are the issue. Password presence is validating on every update. Since User#password is nil and you don't have a password in your params, it fails.
class User < ApplicationRecord
has_secure_password
validates :password, presence: true, confirmation: true
end
>> User.create(email: 'test#user.com', password: '123456');
>> User.first.update!(email: 'test#user.com')
User Load (0.8ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT $1 [["LIMIT", 1]]
ActiveRecord::RecordInvalid: Validation failed: Password can't be blank
has_secure_password also adds its own validations.
https://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html#method-i-has_secure_password
If you want to customize password validations use
has_secure_password validations: false
# + your password validations
To get some ideas, you can take a look at how devise does validations:
https://github.com/heartcombo/devise/blob/v4.8.1/lib/devise/models/validatable.rb#L60
I can not be totally sure without knowing the data in your database but I would say you have two users with username=newperson.
When you try to save any change on any of those the validation triggers and the changes are not commited to the dabase.
I noticed that I have always had issues when it came to logging into my Rails app when using the Brave Browser in private model. When I referred to this comment, this helped me fix the issue in development I believe (or at least temporarily), but after pushing this change to production, I'm running into the same issue.
When I login, I see the following in the Rails console:
I, [2020-12-24T19:43:48.736777 #228] INFO -- : [2d546938-c52a-465f-8269-6b0a5871cc8d] Started POST "/users/sign_in" for 71.199.129.20 at 2020-12-24 19:43:48 +0000
I, [2020-12-24T19:43:48.737556 #228] INFO -- : [2d546938-c52a-465f-8269-6b0a5871cc8d] Processing by Users::SessionsController#create as HTML
I, [2020-12-24T19:43:48.737613 #228] INFO -- : [2d546938-c52a-465f-8269-6b0a5871cc8d] Parameters: {"utf8"=>"✓", "authenticity_token"=>"a94x1G4PTP/UUs571hnYPenK0+UN6vK3+a+0WGY1GlNkXDLO42Xvpq3ph9cXm+yvdyxNBz/Clz9dDjPLk1Ii3g==", "user"=>{"email"=>"myuser#domain.io", "password"=>"[FILTERED]", "remember_me"=>"0"}, "button"=>""}
W, [2020-12-24T19:43:48.746737 #228] WARN -- : [2d546938-c52a-465f-8269-6b0a5871cc8d] Can't verify CSRF token authenticity.
D, [2020-12-24T19:43:48.748787 #228] DEBUG -- : [2d546938-c52a-465f-8269-6b0a5871cc8d] User Load (1.2ms) SELECT "users".* FROM "users" WHERE "users"."email" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["email", "myuser#domain.io"], ["LIMIT", 1]]
D, [2020-12-24T19:43:48.860181 #228] DEBUG -- : [2d546938-c52a-465f-8269-6b0a5871cc8d] (1.5ms) BEGIN
...
D, [2020-12-24T19:43:48.881760 #228] DEBUG -- : [2d546938-c52a-465f-8269-6b0a5871cc8d] (1.9ms) SELECT MAX("audits"."version") FROM "audits" WHERE "audits"."auditable_id" = $1 AND "audits"."auditable_type" = $2 [["auditable_id", 1], ["auditable_type", "User"]]
D, [2020-12-24T19:43:48.883564 #228] DEBUG -- : [2d546938-c52a-465f-8269-6b0a5871cc8d] Audited::Audit Create (1.1ms) INSERT INTO "audits" ("auditable_id", "auditable_type", "user_id", "user_type", "action", "audited_changes", "version", "remote_address", "request_uuid", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" [["auditab$
e_id", 1], ["auditable_type", "User"], ["user_id", 1], ["user_type", "User"], ["action", "update"], ["audited_changes", "---\nauthy_code:\n- 8c3249\n- bbd5cb\n"], ["version", 1702], ["remote_address", "71.199.129.20"], ["request_uuid", "2d546938-c52a-465f-8269-6b0a5871cc8d"], ["created_at", "2020-12-24 19:43:48.879538"]] D, [2020-12-24T19:43:48.885321 #228] DEBUG -- : [2d546938-c52a-465f-8269-6b0a5871cc8d] Consultant Update (1.1ms) UPDATE "users" SET "updated_at" = $1, "authy_code" = $2 WHERE "users"."id" = $3 [["updated_at", "2020-12-24 19:43:48.878303"], ["authy_code", "bbd5cb"], ["id", 1]]
D, [2020-12-24T19:43:48.887646 #228] DEBUG -- : [2d546938-c52a-465f-8269-6b0a5871cc8d] (1.9ms) COMMIT
I, [2020-12-24T19:43:48.888397 #228] INFO -- : [2d546938-c52a-465f-8269-6b0a5871cc8d] Redirected to https://app.domain.io/authentication_method
I, [2020-12-24T19:43:48.888515 #228] INFO -- : [2d546938-c52a-465f-8269-6b0a5871cc8d] Completed 302 Found in 151ms (ActiveRecord: 24.2ms)
I, [2020-12-24T19:43:48.926474 #228] INFO -- : [3b441671-ef25-4483-8623-ae9d33820733] Started GET "/authentication_method" for 71.199.129.20 at 2020-12-24 19:43:48 +0000 I, [2020-12-24T19:43:48.927153 #228] INFO -- : [3b441671-ef25-4483-8623-ae9d33820733] Processing by CommonController#authentication_method as HTML
I, [2020-12-24T19:43:48.935237 #228] INFO -- : [3b441671-ef25-4483-8623-ae9d33820733] Completed 401 Unauthorized in 8ms (ActiveRecord: 4.9ms)
I know the issue is related to the CSRF token, but even disabling the Brave shield in private mode doesn't seem to do the trick.
When I look into Brave's cookies for the domain, I see the following:
Here's what I have in my config/initializers/session_store.rb file:
# frozen_string_literal: true
Rails.application.config.session_store :active_record_store, key: "_my_company_session_#{Rails.env}", domain: :all, tld_length: 2
How can I fix this? I'm assuming that the issue here is that there are duplicate cookies being assigned when browsing to the app, one for the .domain.io domain and the other for app.domain.io domain.
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
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/
I have a rails app that I've been working on locally and have deployed to heroku. I have a lot of data that needs to be seeded that has worked well locally, with a small issue that I didn't pay much mind to. Basically I have a number of tables covering:
Feature,
Addon,
Budget,
ProjectType
Industry
etc.
When I create/reset the local database and run the rake db:seed it seeds perfectly to each database.
However, as its a WIP I keep adding new tables and running the seed rake db:seed, and noticed that where it should have been doubling up all of the seeded data (because I never cleared the data), it actually only duplicated this info on the Addon, and Feature tables. The other tables were unchanged. I thought nothing of it, until I started trying to run heroku run rake db:seed to populate my production database and noticed that in the logs it was also only populating the Addon and Feature tables, and skipping over the rest.
An extract from some of my seed file (I've shortened with ... as they are very repetitive but all the same):
#Populate the features table
Feature.destroy_all
Feature.create(id: 1, name: 'Contact form')
Feature.create(id: 2, name: 'Blog')
Feature.create(id: 3, name: 'Mailing list signup')
...
...
#Populate the addons table
Addon.destroy_all
Addon.create(id: 1, name: 'Domain registration')
Addon.create(id: 2, name: 'Hosting')
Addon.create(id: 3, name: 'Create content')
...
...
#Populating the industries table
Industry.destroy_all
Industry.create(id: 1, name: 'Accounting')
Industry.create(id: 2, name: 'Airlines')
Industry.create(id: 3, name: 'Alternative Medicine')
...
...
# Populating the budgets table
Budget.destroy_all
Budget.create(id: 1, name: 'Micro', minimum: 250, maximum: 1000)
Budget.create(id: 2, name: 'Simple', minimum: 1000, maximum: 2500)
...
...
I noticed that when I try populate the heroku database with heroku run rake db:seed the data all seems to "rollback". Here is an extract from the console:
First I run heroku run rake db:migrate
D, [2016-09-01T12:24:31.463009 #3] DEBUG -- : (1.8ms) SELECT pg_try_advisory_lock(4467995963834188590);
D, [2016-09-01T12:24:31.476939 #3] DEBUG -- : ActiveRecord::SchemaMigration Load (1.9ms) SELECT "schema_migrations".* FROM "schema_migrations"
D, [2016-09-01T12:24:31.507280 #3] DEBUG -- : ActiveRecord::InternalMetadata Load (1.8ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2 [["key", :environment], ["LIMIT", 1]]
D, [2016-09-01T12:24:31.518500 #3] DEBUG -- : (1.7ms) BEGIN
D, [2016-09-01T12:24:31.522494 #3] DEBUG -- : (1.7ms) COMMIT
D, [2016-09-01T12:24:31.524504 #3] DEBUG -- : (1.8ms) SELECT pg_advisory_unlock(4467995963834188590)
Then I run rake heroku run rake db:seed
For the tables where it doesn't work it seems to do this:
D, [2016-09-01T12:27:28.229540 #3] DEBUG -- : ActiveRecord::SchemaMigration Load (2.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
D, [2016-09-01T12:27:28.261528 #3] DEBUG -- : Budget Load (2.3ms) SELECT "budgets".* FROM "budgets"
D, [2016-09-01T12:27:28.293954 #3] DEBUG -- : (1.7ms) BEGIN
D, [2016-09-01T12:27:28.320090 #3] DEBUG -- : (1.9ms) ROLLBACK
D, [2016-09-01T12:27:28.322421 #3] DEBUG -- : (1.7ms) BEGIN
D, [2016-09-01T12:27:28.325418 #3] DEBUG -- : (1.8ms) ROLLBACK
D, [2016-09-01T12:27:28.327617 #3] DEBUG -- : (1.7ms) BEGIN
D, [2016-09-01T12:27:28.331031 #3] DEBUG -- : (1.8ms) ROLLBACK
D, [2016-09-01T12:27:28.333423 #3] DEBUG -- : (1.8ms) BEGIN
D, [2016-09-01T12:27:28.338379 #3] DEBUG -- : (2.5ms) ROLLBACK
D, [2016-09-01T12:27:28.340601 #3] DEBUG -- : (1.7ms) BEGIN
D, [2016-09-01T12:27:28.344061 #3] DEBUG -- : (1.7ms) ROLLBACK
D, [2016-09-01T12:27:28.346208 #3] DEBUG -- : (1.7ms) BEGIN
D, [2016-09-01T12:27:28.349342 #3] DEBUG -- : (1.8ms) ROLLBACK
D, [2016-09-01T12:27:28.353205 #3] DEBUG -- : (3.4ms) BEGIN
D, [2016-09-01T12:27:28.358254 #3] DEBUG -- : (2.5ms) ROLLBACK
D, [2016-09-01T12:27:28.360392 #3] DEBUG -- : (1.7ms) BEGIN
D, [2016-09-01T12:27:28.363406 #3] DEBUG -- : (1.8ms) ROLLBACK
D, [2016-09-01T12:27:28.365488 #3] DEBUG -- : (1.6ms) BEGIN
D, [2016-09-01T12:27:28.367862 #3] DEBUG -- : (1.7ms) ROLLBACK
D, [2016-09-01T12:27:28.369869 #3] DEBUG -- : (1.7ms) BEGIN
D, [2016-09-01T12:27:28.372657 #3] DEBUG -- : (1.8ms) ROLLBACK
D, [2016-09-01T12:27:28.378093 #3] DEBUG -- : Industry Load (2.1ms) SELECT "industries".* FROM "industries"
D, [2016-09-01T12:27:28.393455 #3] DEBUG -- : (1.8ms) BEGIN
D, [2016-09-01T12:27:28.409125 #3] DEBUG -- : (1.7ms) ROLLBACK
D, [2016-09-01T12:27:28.411280 #3] DEBUG -- : (1.6ms) BEGIN
D, [2016-09-01T12:27:28.414223 #3] DEBUG -- : (1.6ms) ROLLBACK
D, [2016-09-01T12:27:28.416244 #3] DEBUG -- : (1.7ms) BEGIN
D, [2016-09-01T12:27:28.419335 #3] DEBUG -- : (1.7ms) ROLLBACK
D, [2016-09-01T12:27:28.421511 #3] DEBUG -- : (1.7ms) BEGIN
D, [2016-09-01T12:27:28.425412 #3] DEBUG -- : (1.7ms) ROLLBACK
D, [2016-09-01T12:27:28.427570 #3] DEBUG -- : (1.7ms) BEGIN
D, [2016-09-01T12:27:28.431136 #3] DEBUG -- : (1.7ms) ROLLBACK
D, [2016-09-01T12:27:28.433536 #3] DEBUG -- : (2.0ms) BEGIN
D, [2016-09-01T12:27:28.438208 #3] DEBUG -- : (1.7ms) ROLLBACK
D, [2016-09-01T12:27:28.440437 #3] DEBUG -- : (1.7ms) BEGIN
And then when it passes over the features and addons, it seems to work fine:
D, [2016-09-01T12:27:29.506570 #3] DEBUG -- : SQL (1.9ms) INSERT INTO "features" ("id", "name", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["id", 1], ["name", "Contact form"], ["created_at", 2016-09-01 12:27:29 UTC], ["updated_at", 2016-09-01 12:27:29 UTC]]
D, [2016-09-01T12:27:29.509515 #3] DEBUG -- : (2.5ms) COMMIT
D, [2016-09-01T12:27:29.512944 #3] DEBUG -- : (2.2ms) BEGIN
D, [2016-09-01T12:27:29.516551 #3] DEBUG -- : SQL (1.9ms) INSERT INTO "features" ("id", "name", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["id", 2], ["name", "Blog"], ["created_at", 2016-09-01 12:27:29 UTC], ["updated_at", 2016-09-01 12:27:29 UTC]]
D, [2016-09-01T12:27:29.519489 #3] DEBUG -- : (2.5ms) COMMIT
D, [2016-09-01T12:27:29.522216 #3] DEBUG -- : (2.5ms) BEGIN
D, [2016-09-01T12:27:29.526125 #3] DEBUG -- : SQL (1.9ms) INSERT INTO "features" ("id", "name", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["id", 3], ["name", "Mailing list signup"], ["created_at", 2016-09-01 12:27:29 UTC], ["updated_at", 2016-09-01 12:27:29 UTC]]
I did notice that of the tables, Addons and Features both have a has_and_belongs_to_many association with other tables while the rest of the tables that aren't working have a belongs_to association with some other tables. Not sure if that is just a coincidence? Thanks for any help!
Ok from what I can tell this appears to be an issue with seeding databases of belongs_to models. I worked around this by:
Going into each model and #'ing the belongs_to relationship. I then repushed with:
git add .
git commit -m "hashed belongs_to associations"
git push
git push heroku master
heroku run rake db:seed
When I checked the database, it's seems to be perfect now. I will now unhash the relationships, re git and re push to heroku.
Why dont you try using find_or_create_by! method which will first check if the record exist and only create if it does not exist.:
Feature.find_or_create_by!(name: 'Contact form')
Also it will take care of validation when you use the bang sign at the end of the method. you also do not have to mention the id here. As you are providing the id, that might be creating an issue for you.
Let me know if you still face any issue.
Since Rails 5.1 belongs_to-associated objects are required by default (see https://blog.bigbinary.com/2016/02/15/rails-5-makes-belong-to-association-required-by-default.html). Maybe some of the belongs_to-associations of your seeded models are not existing, that's why heroku won’t seed the models since presence-validation fails.
One easy solution is to flag the associations as optional:
class Budget
belongs_to :feature, optional: true
....
end