I've been working on a rails application but recently I encountered this error and I have no idea why it's happening, it only happens in my development environment, in production it works just fine. It involves querying data from an inner join, here's the error:
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 ?
Which occurs in this line in the view (In this page I'm trying to show all questions corresponding to the current project):
<%if @proyecto.answers.exists?(question_id: pregunta.id) %>
Here's all the relevant lines of all models and controllers: ANSWER model
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 controller:
class AnswersController < ApplicationController
before_action :require_user
before_action :get_current_project
...
ANSWERS_PROJECTS model:
class AnswersProject < ActiveRecord::Base
belongs_to :project, :dependent => :destroy
has_many :answer, :dependent => :destroy
end
ANSWERS_PROJECTS controller (no contrains):
class AnswersProjectsController < ApplicationController
end
PROJECT model
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 controller
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]
QUESTION model:
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? }
QUESTION controller:
class QuestionsController < ApplicationController
before_action :require_user
before_action :require_project
before_action :require_user, except: [:new, :create]
before_action :current_project, only: [:index]
My code is the exact same that is running in production, I have tried rolling back multiple versions and cloning the repository again, reseting and recreating the database and manually inserting values into each of those tables and the error still persists, it appeared seemingly at random since just yesterday I could run test in development just fine so any help will be greatly appreciated.
[UPDATE]
The heroku logs and my local console logs are different when trying to access the exact same view with the exact same code:
HEROKU Logs:
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 Logs:
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)
As you can see this two sentences produce different results with the same code in different environments, maybe the error is somewhere around here?:
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]]
Development: 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]
If I try to access the same view with no questions created the view renders, but after a single question is create the error appears.