Heroku部署上的ActiveModel :: MissingAttributeError(无法写入未知属性`user_id`)

时间:2018-07-11 13:34:31

标签: ruby-on-rails ruby heroku

我有一个应用程序,其功能类似于Stackoverflow的简化版本(询问,回答,投票)。它在我的计算机上可以正常工作,但是当我在Heroku上部署它时,我在Heroku日志中收到此错误:

ActiveModel :: MissingAttributeError(无法写入未知属性user_id)。

在Heroku上,应用程序告诉我“出了点问题,请检查日志”

我的问题是user_id确实存在于我的表中。所以我真的不知道为什么它不知道这个属性。我也可以登录并没有问题地创建用户。但是,当我想提出一个新问题时,我会收到此错误。...

问题控制器:

class QuestionsController < ApplicationController
  before_action :private_access, except: [:index, :show]

    def index
      @questions = if params[:term]
      Question.where("title iLIKE ? OR description iLIKE ?" , "%#{params[:term]}%", "%#{params[:term]}%")
      else 
      @questions = Question.all.order("updated_at DESC")
      end
    end 

    def new
      @question = Question.new
    end

     def create
      @question = Question.new(questions_params)
      @question.user = current_user
      if @question.save
        flash[:success] = "Fantastic #{@current_user.username} you posed a new question. Surely someone answers soon, stay tuned..."
        redirect_to root_path
      else
         render :new
      end
    end

     def show
        @question = Question.find(params[:id])
        @answer = Answer.new        
        @comment = Comment.new
        @vote = Vote.new
     end 

    def edit
      @question = Question.find(params[:id])
    end

    def update
      @question = Question.find(params[:id])
      if @question.update(questions_params)
      flash[:success] = "Bravo #{@current_user.username}, you updated your question!"
      redirect_to root_path
      else
      flash[:danger] = "Ups, something went wrong. Please try again..."
      render :new
      end
    end


    def destroy
     @question = Question.find(params[:id])
      if @question.destroy
      flash[:danger] = "Ok #{@current_user.username}, question was deleted!"
      redirect_to root_path
      else 
      flash[:danger] = "Ups, something went wrong. Please try again..."
      redirect_to root_path
      end 
    end


    def voteup
      question = Question.find(params[:id])
      question.votes.create(user: current_user)

      flash[:success] = "Thanks #{current_user.username} for voting!"
      redirect_to question_path
    end 


    def votedown
      question = Question.find(params[:id])
      question.votes.where(user: current_user).take.try(:destroy)

      flash[:danger] = "Vote deleted!"
      redirect_to question_path

    end 



    private
        def questions_params
            params.require(:question).permit(:title, :description, :answer_id, :user_id, :body, :votes, :term)
        end 

end

这是我的架构:

   create_table "answers", force: :cascade do |t|
    t.text "body"
    t.bigint "question_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.bigint "user_id"
    t.index ["question_id"], name: "index_answers_on_question_id"
    t.index ["user_id"], name: "index_answers_on_user_id"
  end

  create_table "comments", force: :cascade do |t|
    t.string "commentable_type"
    t.integer "commentable_id"
    t.integer "user_id"
    t.text "body"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "questions", force: :cascade do |t|
    t.string "title"
    t.text "description"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.bigint "user_id"
    t.index ["user_id"], name: "index_questions_on_user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "email", limit: 100
    t.string "password_digest"
    t.string "name", limit: 100
    t.string "username", limit: 50
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "votes", force: :cascade do |t|
    t.integer "voteable_id"
    t.string "voteable_type"
    t.string "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_foreign_key "answers", "questions"
end

我的模型关联应该很好:

class User < ApplicationRecord
has_secure_password validations: false 

has_many :answers, dependent: :destroy
has_many :questions, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :votes, dependent: :destroy



validates :email, uniqueness: true, format: /@/
validates :password, presence: true, on: :create
validates :password, length: {in: 6..20 }, allow_nil: true
validates :name, presence: true
validates :username, presence: true

结束

class Question < ApplicationRecord
validates :title, presence: true
validates :description, presence: true, length: {minimum:10, maximum:10000}

belongs_to :user
has_many :comments, as: :commentable, dependent: :destroy
has_many :answers, dependent: :destroy
has_many :votes, as: :voteable, dependent: :destroy

到目前为止我尝试过的:

  • 重新启动Heroku
  • 重新加载架构(db:schema:load)
  • 哭泣

我们非常感谢您的帮助。提前非常感谢

编辑:我试图重置数据库,删除了该应用程序,然后再次迁移....似乎Heroku没有在问题中添加任何user_id,还是我错了吗?它给了我以下内容

CREATE TABLE "questions" ("id" bigserial primary key, "title" character varying, "description" text, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)
   -> 0.0137s
== 20180620183722 CreateQuestions: migrated (0.0138s) =========================

D, [2018-07-11T14:56:58.545105 #4] DEBUG -- :   ActiveRecord::SchemaMigration Create (1.6ms)  INSERT INTO "schema_migrations" ("version") VALUES ($1) RETURNING "version"  [["version", "20180620183722"]]
D, [2018-07-11T14:56:58.548517 #4] DEBUG -- :    (3.0ms)  COMMIT
I, [2018-07-11T14:56:58.548687 #4]  INFO -- : Migrating to CreateAnswers (20180624115634)
D, [2018-07-11T14:56:58.550810 #4] DEBUG -- :    (1.3ms)  BEGIN
== 20180624115634 CreateAnswers: migrating ====================================
-- create_table(:answers)
D, [2018-07-11T14:56:58.562192 #4] DEBUG -- :    (10.4ms)  CREATE TABLE "answers" ("id" bigserial primary key, "body" text, "question_id" bigint, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_3d5ed4418f"
FOREIGN KEY ("question_id")
  REFERENCES "questions" ("id")
)
D, [2018-07-11T14:56:58.573617 #4] DEBUG -- :    (4.7ms)  CREATE  INDEX  "index_answers_on_question_id" ON "answers"  ("question_id")
   -> 0.0228s
== 20180624115634 CreateAnswers: migrated (0.0229s) ===========================

D, [2018-07-11T14:56:58.577497 #4] DEBUG -- :   ActiveRecord::SchemaMigration Create (2.6ms)  INSERT INTO "schema_migrations" ("version") VALUES ($1) RETURNING "version"  [["version", "20180624115634"]]
D, [2018-07-11T14:56:58.580639 #4] DEBUG -- :    (2.6ms)  COMMIT
I, [2018-07-11T14:56:58.580776 #4]  INFO -- : Migrating to CreateUsers (20180705164230)
D, [2018-07-11T14:56:58.582856 #4] DEBUG -- :    (1.3ms)  BEGIN
== 20180705164230 CreateUsers: migrating ======================================
-- create_table(:users)
D, [2018-07-11T14:56:58.596461 #4] DEBUG -- :    (12.6ms)  CREATE TABLE "users" ("id" bigserial primary key, "email" character varying(100), "password_digest" character varying, "name" character varying(100), "username" character varying(50), "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)
   -> 0.0135s
== 20180705164230 CreateUsers: migrated (0.0137s) =============================

D, [2018-07-11T14:56:58.603402 #4] DEBUG -- :   ActiveRecord::SchemaMigration Create (1.5ms)  INSERT INTO "schema_migrations" ("version") VALUES ($1) RETURNING "version"  [["version", "20180705164230"]]
D, [2018-07-11T14:56:58.606167 #4] DEBUG -- :    (2.1ms)  COMMIT
I, [2018-07-11T14:56:58.606393 #4]  INFO -- : Migrating to CreateComments (20180708124323)
D, [2018-07-11T14:56:58.609250 #4] DEBUG -- :    (1.4ms)  BEGIN
== 20180708124323 CreateComments: migrating ===================================
-- create_table(:comments)
D, [2018-07-11T14:56:58.622279 #4] DEBUG -- :    (9.5ms)  CREATE TABLE "comments" ("id" bigserial primary key, "commentable_type" character varying,"commentable_id" integer, "user_id" integer, "body" text, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)
   -> 0.0129s
== 20180708124323 CreateComments: migrated (0.0130s) ==========================

D, [2018-07-11T14:56:58.624708 #4] DEBUG -- :   ActiveRecord::SchemaMigration Create (1.4ms)  INSERT INTO "schema_migrations" ("version") VALUES ($1) RETURNING "version"  [["version", "20180708124323"]]
D, [2018-07-11T14:56:58.627539 #4] DEBUG -- :    (2.5ms)  COMMIT
I, [2018-07-11T14:56:58.627685 #4]  INFO -- : Migrating to CreateVotes (20180709152735)
D, [2018-07-11T14:56:58.629887 #4] DEBUG -- :    (1.3ms)  BEGIN
== 20180709152735 CreateVotes: migrating ======================================
-- create_table(:votes)
D, [2018-07-11T14:56:58.639342 #4] DEBUG -- :    (8.6ms)  CREATE TABLE "votes" ("id" bigserial primary key, "voteable_id" integer, "voteable_type" character varying, "user_id" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)
   -> 0.0094s
== 20180709152735 CreateVotes: migrated (0.0095s) =============================

D, [2018-07-11T14:56:58.642138 #4] DEBUG -- :   ActiveRecord::SchemaMigration Create (1.4ms)  INSERT INTO "schema_migrations" ("version") VALUES ($1) RETURNING "version"  [["version", "20180709152735"]]
D, [2018-07-11T14:56:58.644622 #4] DEBUG -- :    (2.1ms)  COMMIT
D, [2018-07-11T14:56:58.656312 #4] DEBUG -- :   ActiveRecord::InternalMetadata Load (1.5ms)  SELECT  "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2  [["key", "environment"], ["LIMIT", 1]]
D, [2018-07-11T14:56:58.671265 #4] DEBUG -- :    (5.5ms)  BEGIN
D, [2018-07-11T14:56:58.674884 #4] DEBUG -- :   ActiveRecord::InternalMetadata Create (1.7ms)  INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "key"  [["key", "environment"], ["value", "production"], ["created_at", "2018-07-11 14:56:58.671911"], ["updated_at", "2018-07-11 14:56:58.671911"]]
D, [2018-07-11T14:56:58.677921 #4] DEBUG -- :    (2.6ms)  COMMIT
D, [2018-07-11T14:56:58.680372 #4] DEBUG -- :    (2.1ms)  SELECT pg_advisory_unlock(2612648823889037010)
➜  RubyOnCrack git:(master) ✗

2 个答案:

答案 0 :(得分:0)

我想也许错误正在这里抛出?

def create
  @question = Question.new(questions_params)
  @question.user = current_user
  if @question.save
    flash[:success] = "Fantastic #{@current_user.username} you posed a new question. Surely someone answers soon, stay tuned..."
    redirect_to root_path
  else
     render :new
  end
end

出于好奇,您是否尝试过:

def create
  @question = current_user.questions.build(questions_params)
  if @question.save
    flash[:success] = "Fantastic #{@current_user.username} you posed a new question. Surely someone answers soon, stay tuned..."
    redirect_to root_path
  else
     render :new
  end
end

如果这不起作用,您是否可以在控制台中创建新记录?

答案 1 :(得分:0)

我在您的帮助下进行了修复。我需要做的是:

  1. 完全删除并重建我的本地数据库
  2. 再次添加两次迁移,将用户添加到问题和答案中,因为出于某种原因进行的重置未实现
  3. 删除Heroku应用并再次部署

现在它正在工作。无论如何,如果您想签出此应用程序的链接,请使用以下链接:https://rubyoncrack.herokuapp.com/