INNER连接上的Rails NoSuchColumn错误

时间:2018-02-03 03:22:19

标签: ruby-on-rails ruby activerecord sqlexception sqlite3-ruby

我一直在使用rails应用程序,但最近我遇到了这个错误,我不知道它为什么会发生,它只发生在我的开发环境中,在生产中它运行得很好。 它涉及从内部联接查询数据,这里是错误:

SQLite3::SQLException: no such column: answers.answers_project_id: SELECT  1 AS one FROM "answers" INNER JOIN "answers_projects" ON "answers"."answers_project_id" = "answers_projects"."id" WHERE "answers_projects"."project_id" = ? AND "answers"."question_id" = ? LIMIT ?

在视图的这一行中出现(在此页面中,我试图显示与当前项目相对应的所有问题):

<%if @proyecto.answers.exists?(question_id: pregunta.id) %>

这里是所有模型和控制器的所有相关行: 答案模型

class Answer < ActiveRecord::Base
  belongs_to :question
    has_many :answers_projects
    has_many :projects, through: :answers_projects

    validates :answer, presence: true, length: {minimum: 1}

    def get_question_text
        Question.where(id: self.id).question
    end

end

ANSWER控制器:

    class AnswersController < ApplicationController
   before_action :require_user
   before_action :get_current_project
...

ANSWERS_PROJECTS模型:

class AnswersProject < ActiveRecord::Base
   belongs_to :project, :dependent => :destroy
    has_many :answer, :dependent => :destroy
end 

ANSWERS_PROJECTS控制器(无禁忌):

class AnswersProjectsController < ApplicationController

end

PROJECT模型

class Project < ActiveRecord::Base
    belongs_to :user
    has_many :answers_projects, :dependent => :destroy
    has_many :answers, through: :answers_projects, :dependent => :destroy
    accepts_nested_attributes_for :answers

PROJECT控制器

class ProjectsController < ApplicationController
   before_action :set_project, only: [:edit,:update,:show,:destroy]
    before_action :require_user
    before_action :require_same_user, only: [:edit, :show, :update, :destroy]
    before_action :require_admin, only: [:edit,:update,:destroy]

问题模型:

class Question < ApplicationRecord  
    has_many :options, dependent: :destroy
        has_many :answers, dependent: :destroy
    accepts_nested_attributes_for :options, allow_destroy: true, reject_if: proc { |att| att['description'].blank? }

问题控制器:

class QuestionsController < ApplicationController
    before_action :require_user
    before_action :require_project
    before_action :require_user, except: [:new, :create]
    before_action :current_project, only: [:index]

我的代码与生产中的代码完全相同,我尝试回滚多个版本并再次克隆存储库,重置并重新创建数据库并手动将值插入每个表中,错误仍然存​​在,它出现了从昨天起我就可以随意进行测试了,所以任何帮助都会受到高度赞赏 [UPDATE]
尝试使用完全相同的代码访问完全相同的视图时,heroku日志和我的本地控制台日志不同:


HEROKU日志:

2018-02-03T03:35:49.030139+00:00 app[web.1]: [97a6f665-a618-4d88-b018-0b3329e6b4c5] Processing by QuestionsController#index as 
2018-02-03T03:35:49.085812+00:00 app[web.1]: [97a6f665-a618-4d88-b018-0b3329e6b4c5]   User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
2018-02-03T03:35:49.117275+00:00 app[web.1]: [97a6f665-a618-4d88-b018-0b3329e6b4c5]    (0.9ms)  SELECT COUNT(*) FROM "projects" WHERE "projects"."user_id" = $1  [["user_id", 1]]
2018-02-03T03:35:49.121949+00:00 app[web.1]: [97a6f665-a618-4d88-b018-0b3329e6b4c5]   Project Load (0.6ms)  SELECT  "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
2018-02-03T03:35:49.148596+00:00 app[web.1]: [97a6f665-a618-4d88-b018-0b3329e6b4c5]   Rendering questions/index.html.erb within layouts/application
2018-02-03T03:35:49.161477+00:00 app[web.1]: [97a6f665-a618-4d88-b018-0b3329e6b4c5]   Question Load (1.7ms)  SELECT "questions".* FROM "questions" ORDER BY "questions"."process" ASC
2018-02-03T03:35:49.204998+00:00 app[web.1]: [97a6f665-a618-4d88-b018-0b3329e6b4c5]   Answer Exists (1.4ms)  SELECT  1 AS one FROM "answers" INNER JOIN "answers_projects" ON "answers"."id" = "answers_projects"."answer_id" WHERE "answers_projects"."project_id" = $1 AND "answers"."question_id" = $2 LIMIT $3  [["project_id", 2], ["question_id", 3], ["LIMIT", 1]]    



MY CONSOLE日志:

User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
   (0.2ms)  SELECT COUNT(*) FROM "projects" WHERE "projects"."user_id" = ?  [["user_id", 1]]
  Project Load (0.2ms)  SELECT  "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT ?  [["id", 3], ["LIMIT", 1]]
  Rendering questions/index.html.erb within layouts/application
  Question Load (0.3ms)  SELECT "questions".* FROM "questions" ORDER BY "questions"."process" ASC
  Answer Exists (0.6ms)  SELECT  1 AS one FROM "answers" INNER JOIN "answers_projects" ON "answers"."answers_project_id" = "answers_projects"."id" WHERE "answers_projects"."project_id" = ? AND "answers"."question_id" = ? LIMIT ?  [["project_id", 3], ["question_id", 34], ["LIMIT", 1]]
  Rendered questions/_answer_form.html.erb (131.3ms)
  Rendered questions/index.html.erb within layouts/application (143.0ms)
Completed 500 Internal Server Error in 418ms (ActiveRecord: 4.8ms)

正如你可以看到这两个句子在不同的环境中使用相同的代码产生不同的结果,可能错误就在这里?: Heroku:Answer Exists (1.4ms) SELECT 1 AS one FROM "answers" INNER JOIN "answers_projects" ON "answers"."id" = "answers_projects"."answer_id" WHERE "answers_projects"."project_id" = $1 AND "answers"."question_id" = $2 LIMIT $3 [["project_id", 2], ["question_id", 3], ["LIMIT", 1]] 发展:Answer Exists (0.6ms) SELECT 1 AS one FROM "answers" INNER JOIN "answers_projects" ON "answers"."answers_project_id" = "answers_projects"."id" WHERE "answers_projects"."project_id" = ? AND "answers"."question_id" = ? LIMIT ? [["project_id", 3], ["question_id", 34], ["LIMIT", 1]]
[UPDATE]
如果我尝试访问相同的视图而没有创建任何问题,则视图呈现,但在创建单个问题后,将显示错误。

1 个答案:

答案 0 :(得分:0)

这是对的吗?

class AnswersProject < ActiveRecord::Base
  belongs_to :project, :dependent => :destroy
  has_many :answer, :dependent => :destroy
end 

AnswersProjects has_many:答案?还是属于belongs_to?如果它是has_many,你必须将它复数。