Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.4K
Visualizações
Rails Includes Error : UndefinedTable: ERROR: missing FROM-clause entry for table

I prefer use includes to connect my three tables. I have three model that I need to connect such as Register, Student, and Schedule. Here is my model Association

class Student < ApplicationRecord
    belongs_to :register
end

class Register < ApplicationRecord
    has_one :student

    belongs_to :schedule
end

class Schedule < ApplicationRecord
    belongs_to :course
    belongs_to :teacher

    has_many :days, :dependent => :destroy, :class_name => 'ScheduleDay'
    has_many :registers
end

Here is my controller

def index
        @students = Student.includes([register: :schedule])
        @students = @students.order 'students.created_at DESC'


        if params[:status_id] && params[:status_id].to_i > 0
            @students = @students.where 'students.status_id = ?', params[:status_id]
        end

        if params[:id] && params[:id].to_i > 0
            @students = @students.where 'cast(students.id as varchar) like (?)', "%#{params[:id]}%"
        end

        if params[:full_name] && params[:full_name].to_s.length > 0
            @students = @students.where 'lower(registers.name_in_indonesian || registers.name_in_chinese) like lower(?)', "%#{params[:full_name]}%"
        end

        if params[:course_id] && params[:course_id].to_i > 0
            @students = @students.where 'schedules.course_id = ?', params[:course_id]
        end

        @students = @students.paginate(page: params[:page], per_page: 30)
end

I got following error :

PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "schedules"
LINE 1: SELECT "students".* FROM "students" WHERE (schedules.course_...

I tried to debug by using rails console but end up with (Object doesn't support #inspect) enter image description here

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You need to join those tables if you want to query them, includes is not enough:

@students = Student.joins(register: :schedule)

or add a references call:

@students = Student.includes(register: :schedule).references(register: :schedule)

includes will reduce the number of database queries to access the included tables but it won't JOIN the tables.

over 4 years ago · Santiago Trujillo Relatório

0

I think you are having some unmigrated migrations. Please run

rake db:migrate

After running above command restart rails console and then try the same thing. You will not be getting this error.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda