1

my code is working fine on my system in development mode but when i pushed to heroku, i am getting this error on the logs.

i'm on Rails 5.2.3 & ruby 2.3.3

ActionView::Template::Error (PG::UndefinedColumn: ERROR: column posts.user_id does not exist

LINE 1: SELECT COUNT(*) FROM "posts" WHERE "posts"."user_id" = $1

On heroku console, when i try to retrieve user_id i get

irb(main):001:0> p = Post.last
  Post Load (11.0ms)  SELECT  "posts".* FROM "posts" ORDER BY "posts"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> nil
irb(main):002:0> p.user_id
Traceback (most recent call last):
        1: from (irb):2
NoMethodError (undefined method `user_id' for nil:NilClass)
irb(main):003:0>  !    ECONNRESET: read ECONNRESET

but on development i get

irb(main):001:0> p =Post.last
  Post Load (0.3ms)  SELECT  "posts".* FROM "posts" ORDER BY "posts"."id" DESC LIMIT ?  [["LIMIT", 1]]
=> #<Post id: 13, description: "#snopp", user_id: 1, created_at: "2019-05-02 15:38:09", updated_at: "2019-05-02 15:38:09", image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil>
irb(main):002:0> p.user_id
=> 1
irb(main):003:0>

this is my schema.rb

ActiveRecord::Schema.define(version: 2019_05_02_123348) do

  create_table "active_storage_attachments", force: :cascade do |t|
    t.string "name", null: false
    t.string "record_type", null: false
    t.integer "record_id", null: false
    t.integer "blob_id", null: false
    t.datetime "created_at", null: false
    t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
    t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
  end

  create_table "active_storage_blobs", force: :cascade do |t|
    t.string "key", null: false
    t.string "filename", null: false
    t.string "content_type"
    t.text "metadata"
    t.bigint "byte_size", null: false
    t.string "checksum", null: false
    t.datetime "created_at", null: false
    t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
  end

  create_table "hash_tags", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "post_hash_tags", force: :cascade do |t|
    t.integer "post_id"
    t.integer "hash_tag_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["hash_tag_id"], name: "index_post_hash_tags_on_hash_tag_id"
    t.index ["post_id"], name: "index_post_hash_tags_on_post_id"
  end

  create_table "posts", force: :cascade do |t|
    t.string "description"
    t.integer "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "image_file_name"
    t.string "image_content_type"
    t.bigint "image_file_size"
    t.datetime "image_updated_at"
  end

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer "sign_in_count", default: 0, null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string "current_sign_in_ip"
    t.string "last_sign_in_ip"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "username"
    t.string "name"
    t.string "website"
    t.text "bio"
    t.integer "phone"
    t.string "gender"
    t.string "avatar"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

end

Post Model

class Post < ApplicationRecord
    after_commit :create_hash_tags, on: :create

    has_many :post_hash_tags
    has_many :hash_tags, through: :post_hash_tags



    belongs_to :user
end

user model

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :trackable

         has_many :posts, dependent: :destroy
end
bottles
  • 27
  • 1
  • 9

3 Answers3

2
heroku pg:reset
heroku run rake db:migrate
heroku restart

There is no instances of Post which gives you the error of NilClass. You can seed the database as suggested in the other post. For more information about seeding a database check the rails docs on seeding.

Steven Aguilar
  • 3,107
  • 5
  • 39
  • 89
  • The error doesn't indicate this is the solution. If there were no posts table the error would be `PG::UndefinedTable: ERROR` – lacostenycoder May 02 '19 at 16:39
  • is giving `undefined method 'user_id' for nil:NilClass` which means Heroku logs that error since there is no `Post` instance. Even thought the class is there – Steven Aguilar May 02 '19 at 16:41
  • @lacostenycoder you see!, you down voted a correct answer. – Steven Aguilar May 02 '19 at 16:42
  • @bottles if is correct you mind upvoting the solution. – Steven Aguilar May 02 '19 at 16:42
  • this is what community is all about! Thanks for the feedback @lacostenycoder your input was more in-depth. I clearly understand now why you wanted the correction on seeds. – Steven Aguilar May 02 '19 at 19:54
  • @StevenAguilar also, beware that `heroku pg:reset` will basically destroy the existing db, which might NOT be what the OP wants and is a bit heavy handed. See https://stackoverflow.com/questions/4820549/how-to-empty-a-heroku-database – lacostenycoder May 02 '19 at 19:59
2

To test your application on Heroku which we would assume is running in production mode, you'll probably want to seed the production database.

You should have a db/seeds.rb file where you can do this.

See this answer for ways you can use it. Also see Rails Docs for ideas.

If you build your seeds with seeds.rb file you should be able to run

heroku run rake db:seed
lacostenycoder
  • 10,623
  • 4
  • 31
  • 48
0

not sure if we can set automated migration while deploying to the heroku, but once the code is deployed to the heroku you need to run

heroku run rake db:migrate

and your problem will be solved.

Ashok Damaniya
  • 303
  • 1
  • 6