01 October 2014

Sometimes we need to join the same table twice, for example

# Table name: attendances
#
#  student_id          :integer
#  teacher_id          :integer
#  status              :string

class Attendance < ActiveRecord::Base
  belongs_to :student
  belongs_to :teacher
end

class Student < User
end

class Teacher < User
end

Then you could use as to rename the query table name to join same table twice.

Attendances.
   joins("join users as students on attendances.student_id = students.id
          join users as teachers on attendances.teacher_id = teachers.id")