0

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.

IvanHid
  • 669
  • 7
  • 25

1 Answers1

0

Is this correct?

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

Does AnswersProjects has_many :answers? or should it be belongs_to?. If it's has_many, you must pluralize it.

Pablo
  • 3,004
  • 1
  • 12
  • 19
  • Thanks for pointing that out, AnswersProjects has many answers since it is linking one single project with many answers, I have already pluralized it but the problem persists, could you please check the heroku logs I just added? – IvanHid Feb 03 '18 at 03:53
  • I really think you don't need has_many there, because it's a join table. You have Projects and Answers as the main Models. And AnswersProject is a link between answers and projects. It should have belongs_to project and belongs_to answer and this allows many answers per project, and many projects per answer. I checked the new logs. It seems you don't have the same code. It seems you have answer belongs_to :answers_projects in development (but it's not what you posted) – Pablo Feb 03 '18 at 04:13
  • I changed my has_many to a belongs_to in AnswersProject but the problem persists, I cloned my heroku app into a new folder and I posted the controllers here so it must be the same code as in production, although I had to excecute heroku rollback a couple of times, could it be that the latest pre-rollback version is getting cloned instead of the live old version? Ex: I was on release 26 but I rolled back to 24 maybe the version getting cloned is 26? – IvanHid Feb 03 '18 at 04:21
  • The thing is that the database must reflect your models. You cannot change models without changing the database schema. WIth the last change, you need a project_id and an answer_id in AnswersProjects table. Maybe you have differences between the database schema and your models. – Pablo Feb 03 '18 at 04:28