Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/phlegx/rails_query_alias_names
Rails 4 - Alias names in includes() and joins() for active record quering
https://github.com/phlegx/rails_query_alias_names
Last synced: 3 months ago
JSON representation
Rails 4 - Alias names in includes() and joins() for active record quering
- Host: GitHub
- URL: https://github.com/phlegx/rails_query_alias_names
- Owner: phlegx
- Archived: true
- Created: 2015-02-19T17:15:07.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-02-15T22:12:12.000Z (over 8 years ago)
- Last Synced: 2024-06-03T21:57:32.954Z (5 months ago)
- Language: Ruby
- Size: 19.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rdoc
Awesome Lists containing this project
README
== Rails 4 - Alias names in includes() and joins() for active record quering
A question about this issue was post on stack overflow: http://stackoverflow.com/questions/28595636/rails-4-how-to-give-alias-names-to-includes-and-joins-in-active-record-que.
This gist has more information about the problem: https://gist.github.com/phlegx/add77d24ebc57f211e8b
=== Model design
- Model Student and model Teacher are both STI models with super class model User.
- Model Story is a STI model with super class model Task.
- Model Project has associations to all other models.class Project < ActiveRecord::Base
has_many :users
has_many :students
has_many :teachers
has_many :tasks
has_many :stories
end
class Task < ActiveRecord::Base
belongs_to :project
end
class Story < Task
end
class User < ActiveRecord::Base
belongs_to :project
end
class Student < User
end
class Teacher < User
end
=== Rails alias naming convention (includes() and joins())
- One model as parameter
- is base model (includes(:users))
- alias name is class name of base name (in plural)
- is STI model (includes(:students))
- alias name is class name of base name (in plural)
- More models as parameters
- have more STI models with same base class name (includes(:students, :teachers))
- first parameter (STI) -> alias name is class name of base name (in plural)
- all other parameters (STI) -> alias name is {STI name plural}_projects
- One base model and one STI model with same base model (includes(:users, :teachers))
- first parameter (base) -> alias name is class name of base name (in plural)
- second parameter (STI) -> alias name is {STI name plural}_projects
- More STI models with different base model (includes(:students, :stories))
- alias name is class name of base name (in plural)
The alias name depends on the parameter order in includes() or joins() and if the parameters have another parameter with same base class.==== Case one (single STI model include)
# This examples works:
Project.all.includes(:students).order('users.name ASC') # order on students
Project.all.joins(: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')
Project.all.joins(:students).order('students.name ASC')
Project.all.joins(:students).order('students_projects.name ASC')
==== Case two (double STI model include with same base model)
# This examples works:
Project.all.includes(:students, :teachers).order('users.name ASC') # order on students
Project.all.includes(:students, :teachers).order('teachers_projects.name ASC') # order on teachers
Project.all.includes(:students, :stories).order('tasks.name ASC') # order on stories
Project.all.joins(:students, :teachers).order('users.name ASC') # order on students
Project.all.joins(:students, :teachers).order('teachers_projects.name ASC') # order on teachers
Project.all.joins(:students, :stories).order('tasks.name ASC') # order on stories
# 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')
Project.all.includes(:students, :stories).order('stories.name ASC')
Project.all.joins(:students, :teachers).order('teachers.name ASC')
Project.all.joins(:students, :teachers).order('students.name ASC')
Project.all.joins(:students, :teachers).order('students_projects.name ASC')
Project.all.joins(:students, :stories).order('stories.name ASC')