How can I give an alias name for e.g. includes()
?
Following is given:
- User: active record model
- Student: active record model, inherits from User (STI)
- Teacher: active record model, inherits from User (STI)
- Project: active record model
Here some examples:
FIRST CASE (more STI associations)
Project.all.includes(:students, :teachers).order('teachers_projects.name ASC') # order on teachers
Project.all.includes(:students, :teachers).order('users.name ASC') # order on students
Rails uses automatically alias name teachers_projects
for :teachers
in the SQL. How can I overwrite this, so that I can use alias name teachers
instead of teachers_projects
in the SQL? :students
gets alias name users
.
This examples fails:
Project.all.includes(:students, :teachers).order('teachers.name ASC')
Project.all.includes(:students, :teachers).order('students.name ASC')
Project.all.includes(:students, :teachers).order('students_projects.name ASC')
SECOND CASE (one STI association)
If I use only :students
(without :teachers
) in method includes()
, Rails uses name alias of the STI base class name users
(without _projects
attached) for :students
:
Project.all.includes(:students).order('users.name ASC') # order on students
This examples fails:
Project.all.includes(:students).order('students.name ASC')
Project.all.includes(:students).order('students_projects.name ASC')
QUESTION
Might exist something like:
Project.all.includes(:students).alias(students: :my_alias)
RAILS ALIAS TRACKER
TESTING APP