Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save phlegx/add77d24ebc57f211e8b to your computer and use it in GitHub Desktop.
Save phlegx/add77d24ebc57f211e8b to your computer and use it in GitHub Desktop.

Revisions

  1. phlegx revised this gist Feb 21, 2015. 1 changed file with 1 addition and 0 deletions.
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,7 @@ The test app:
    Some links:

    - https://github.com/rails/rails/issues/606
    - https://github.com/rails/rails/blob/v4.2.0/activerecord/lib/active_record/associations/alias_tracker.rb#L59
    - http://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-the-joined-tables
    - https://rails.lighthouseapp.com/projects/8994/tickets/5617-patch-sti-join-model-polymorphic-associations-works-now
    - https://groups.google.com/forum/#!topic/rails-oceania/iHlJPSd-lKM
  2. phlegx revised this gist Feb 19, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion tests.rb
    Original file line number Diff line number Diff line change
    @@ -12,5 +12,6 @@
    Project.joins(Project.arel_table.join(Student.arel_table).on(Student.arel_table[:project_id].eq(Project.arel_table[:id]).and(Student.arel_table[:type].in('Student'))).join_sources).to_sql


    p = Arel::Table.new(:projects)
    s = Arel::Table.new(:users, as: 'students')
    Project.joins(Project.arel_table.join(s).on(Project.arel_table[:id].eq(s[:project_id]).and(s[:type].in('Student'))).join_sources)
    Project.joins(p.join(s).on(p[:id].eq(s[:project_id]).and(s[:type].in('Student'))).join_sources)
  3. phlegx revised this gist Feb 19, 2015. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion tests.rb
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,9 @@
    s = Arel::Table.new(:users, as: 'students')
    Project.joins(p.join(s).join_sources)

    Project.joins(Project.arel_table.join(Student.arel_table).on(Student.arel_table[:project_id].eq(Project.arel_table[:id])).join_sources).to_sql

    Project.joins(Project.arel_table.join(Student.arel_table).on(Student.arel_table[:project_id].eq(Project.arel_table[:id]).and(Student.arel_table[:type].in('Student'))).join_sources).to_sql


    s = Arel::Table.new(:users, as: 'students')
    Project.joins(Project.arel_table.join(s).on(Project.arel_table[:id].eq(s[:project_id]).and(s[:type].in('Student'))).join_sources)
  4. phlegx revised this gist Feb 19, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions tests.rb
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,6 @@
    s = Arel::Table.new(:users, as: 'students')
    Project.joins(p.join(s).join_sources)

    Project.joins(Project.arel_table.join(Student.arel_table).on(Project.arel_table[:id].eq(Student.arel_table[:project_id])).join_sources).to_sql
    Project.joins(Project.arel_table.join(Student.arel_table).on(Student.arel_table[:project_id].eq(Project.arel_table[:id])).join_sources).to_sql

    Project.joins(Project.arel_table.join(Student.arel_table).on(Project.arel_table[:id].eq(Student.arel_table[:project_id]).and(Student.arel_table[:type].in('Student'))).join_sources).to_sql
    Project.joins(Project.arel_table.join(Student.arel_table).on(Student.arel_table[:project_id].eq(Project.arel_table[:id]).and(Student.arel_table[:type].in('Student'))).join_sources).to_sql
  5. phlegx revised this gist Feb 19, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions tests.rb
    Original file line number Diff line number Diff line change
    @@ -9,3 +9,5 @@
    Project.joins(p.join(s).join_sources)

    Project.joins(Project.arel_table.join(Student.arel_table).on(Project.arel_table[:id].eq(Student.arel_table[:project_id])).join_sources).to_sql

    Project.joins(Project.arel_table.join(Student.arel_table).on(Project.arel_table[:id].eq(Student.arel_table[:project_id]).and(Student.arel_table[:type].in('Student'))).join_sources).to_sql
  6. phlegx revised this gist Feb 19, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions tests.rb
    Original file line number Diff line number Diff line change
    @@ -7,3 +7,5 @@

    s = Arel::Table.new(:users, as: 'students')
    Project.joins(p.join(s).join_sources)

    Project.joins(Project.arel_table.join(Student.arel_table).on(Project.arel_table[:id].eq(Student.arel_table[:project_id])).join_sources).to_sql
  7. phlegx revised this gist Feb 19, 2015. 2 changed files with 10 additions and 6 deletions.
    Original file line number Diff line number Diff line change
    @@ -36,9 +36,4 @@ Some links:
    - https://groups.google.com/forum/#!topic/rails-oceania/iHlJPSd-lKM
    - http://stackoverflow.com/questions/15789145/how-to-join-on-subqueries-using-arel

    - https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_gem.rb


    Some tests:

    - Project.joins('INNER JOIN "users" AS "students" ON "students"."project_id" = "projects"."id" AND "students"."type" IN ("Student")').order('students.name DESC')
    - https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_gem.rb
    9 changes: 9 additions & 0 deletions tests.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # Working example

    Project.joins('INNER JOIN "users" AS "students" ON "students"."project_id" = "projects"."id" AND "students"."type" IN ("Student")').order('students.name DESC')


    # Try to transform query to Arel

    s = Arel::Table.new(:users, as: 'students')
    Project.joins(p.join(s).join_sources)
  8. phlegx revised this gist Feb 19, 2015. 1 changed file with 6 additions and 1 deletion.
    Original file line number Diff line number Diff line change
    @@ -36,4 +36,9 @@ Some links:
    - https://groups.google.com/forum/#!topic/rails-oceania/iHlJPSd-lKM
    - http://stackoverflow.com/questions/15789145/how-to-join-on-subqueries-using-arel

    - https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_gem.rb
    - https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_gem.rb


    Some tests:

    - Project.joins('INNER JOIN "users" AS "students" ON "students"."project_id" = "projects"."id" AND "students"."type" IN ("Student")').order('students.name DESC')
  9. phlegx revised this gist Feb 19, 2015. 1 changed file with 1 addition and 0 deletions.
    Original file line number Diff line number Diff line change
    @@ -34,5 +34,6 @@ Some links:
    - http://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-the-joined-tables
    - https://rails.lighthouseapp.com/projects/8994/tickets/5617-patch-sti-join-model-polymorphic-associations-works-now
    - https://groups.google.com/forum/#!topic/rails-oceania/iHlJPSd-lKM
    - http://stackoverflow.com/questions/15789145/how-to-join-on-subqueries-using-arel

    - https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_gem.rb
  10. phlegx revised this gist Feb 19, 2015. 1 changed file with 1 addition and 0 deletions.
    Original file line number Diff line number Diff line change
    @@ -33,5 +33,6 @@ Some links:
    - https://github.com/rails/rails/issues/606
    - http://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-the-joined-tables
    - https://rails.lighthouseapp.com/projects/8994/tickets/5617-patch-sti-join-model-polymorphic-associations-works-now
    - https://groups.google.com/forum/#!topic/rails-oceania/iHlJPSd-lKM

    - https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_gem.rb
  11. phlegx revised this gist Feb 19, 2015. 1 changed file with 4 additions and 0 deletions.
    Original file line number Diff line number Diff line change
    @@ -24,6 +24,10 @@ Rails alias naming convention (includes() and joins())

    The alias name depends on the parameter order in includes() or joins() and if the parameters have another parameter with same base class.

    The test app:

    - https://github.com/phlegx/rails_query_alias_names

    Some links:

    - https://github.com/rails/rails/issues/606
  12. phlegx revised this gist Feb 19, 2015. 1 changed file with 20 additions and 20 deletions.
    40 changes: 20 additions & 20 deletions seeds.rb
    Original file line number Diff line number Diff line change
    @@ -4,19 +4,19 @@


    u1 = User.find_or_create_by!(name: 'First User') do |u|
    s.project = p1
    u.project = p1
    end
    u2 = User.find_or_create_by!(name: 'Second User') do |u|
    s.project = p1
    u.project = p1
    end
    u3 = User.find_or_create_by!(name: 'Third User') do |u|
    s.project = p2
    u.project = p2
    end
    u4 = User.find_or_create_by!(name: 'Fourth User') do |u|
    s.project = p2
    u.project = p2
    end
    u5 = User.find_or_create_by!(name: 'Fifth User') do |u|
    s.project = p3
    u.project = p3
    end


    @@ -38,53 +38,53 @@


    t1 = Teacher.find_or_create_by!(name: 'First Teacher') do |t|
    s.project = p1
    t.project = p1
    end
    t2 = Teacher.find_or_create_by!(name: 'Second Teacher') do |t|
    s.project = p1
    t.project = p1
    end
    t3 = Teacher.find_or_create_by!(name: 'Third Teacher') do |t|
    s.project = p2
    t.project = p2
    end
    t4 = Teacher.find_or_create_by!(name: 'Fourth Teacher') do |t|
    s.project = p2
    t.project = p2
    end
    t5 = Teacher.find_or_create_by!(name: 'Fifth Teacher') do |t|
    s.project = p3
    t.project = p3
    end


    st1 = Story.find_or_create_by!(name: 'First Story') do |st|
    s.project = p1
    st.project = p1
    end
    st2 = Story.find_or_create_by!(name: 'Second Story') do |st|
    s.project = p1
    st.project = p1
    end
    st3 = Story.find_or_create_by!(name: 'Third Story') do |st|
    s.project = p2
    st.project = p2
    end
    st4 = Story.find_or_create_by!(name: 'Fourth Story') do |st|
    s.project = p2
    st.project = p2
    end
    st5 = Story.find_or_create_by!(name: 'Fifth Story') do |st|
    s.project = p3
    st.project = p3
    end


    ta1 = Task.find_or_create_by!(name: 'First Task') do |ta|
    s.project = p1
    ta.project = p1
    end
    ta2 = Task.find_or_create_by!(name: 'Second Task') do |ta|
    s.project = p1
    ta.project = p1
    end
    ta3 = Task.find_or_create_by!(name: 'Third Task') do |ta|
    s.project = p2
    ta.project = p2
    end
    ta4 = Task.find_or_create_by!(name: 'Fourth Task') do |ta|
    s.project = p2
    ta.project = p2
    end
    ta5 = Task.find_or_create_by!(name: 'Fifth Task') do |ta|
    s.project = p3
    ta.project = p3
    end


  13. phlegx revised this gist Feb 19, 2015. 1 changed file with 4 additions and 2 deletions.
    Original file line number Diff line number Diff line change
    @@ -27,5 +27,7 @@ The alias name depends on the parameter order in includes() or joins() and if th
    Some links:

    - https://github.com/rails/rails/issues/606
    - https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_gem.rb
    - http://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-the-joined-tables
    - http://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-the-joined-tables
    - https://rails.lighthouseapp.com/projects/8994/tickets/5617-patch-sti-join-model-polymorphic-associations-works-now

    - https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_gem.rb
  14. phlegx revised this gist Feb 19, 2015. 1 changed file with 2 additions and 1 deletion.
    Original file line number Diff line number Diff line change
    @@ -27,4 +27,5 @@ The alias name depends on the parameter order in includes() or joins() and if th
    Some links:

    - https://github.com/rails/rails/issues/606
    - https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_gem.rb
    - https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_gem.rb
    - http://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-the-joined-tables
  15. phlegx revised this gist Feb 19, 2015. 1 changed file with 2 additions and 1 deletion.
    Original file line number Diff line number Diff line change
    @@ -26,4 +26,5 @@ The alias name depends on the parameter order in includes() or joins() and if th

    Some links:

    - https://github.com/rails/rails/issues/606
    - https://github.com/rails/rails/issues/606
    - https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_gem.rb
  16. phlegx revised this gist Feb 19, 2015. 1 changed file with 5 additions and 1 deletion.
    Original file line number Diff line number Diff line change
    @@ -22,4 +22,8 @@ Rails alias naming convention (includes() and joins())
    - 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.
    The alias name depends on the parameter order in includes() or joins() and if the parameters have another parameter with same base class.

    Some links:

    - https://github.com/rails/rails/issues/606
  17. phlegx revised this gist Feb 19, 2015. 2 changed files with 7 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions migrations.rb
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,7 @@ def change
    end
    end


    class CreateTasks < ActiveRecord::Migration
    def change
    create_table :tasks do |t|
    @@ -18,6 +19,7 @@ def change
    end
    end


    class CreateUsers < ActiveRecord::Migration
    def change
    create_table :users do |t|
    5 changes: 5 additions & 0 deletions models.rb
    Original file line number Diff line number Diff line change
    @@ -8,20 +8,25 @@ class Project < ActiveRecord::Base
    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

  18. phlegx renamed this gist Feb 19, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  19. phlegx revised this gist Feb 19, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions models.rb
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,10 @@
    # all models

    class Project < ActiveRecord::Base
    has_many :users
    has_many :students
    has_many :teachers
    has_many :tasks
    has_many :stories
    end

  20. phlegx revised this gist Feb 19, 2015. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions seeds.rb
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,9 @@
    u4 = User.find_or_create_by!(name: 'Fourth User') do |u|
    s.project = p2
    end
    u5 = User.find_or_create_by!(name: 'Fifth User') do |u|
    s.project = p3
    end


    s1 = Student.find_or_create_by!(name: 'First Student') do |s|
    @@ -29,6 +32,9 @@
    s4 = Student.find_or_create_by!(name: 'Fourth Student') do |s|
    s.project = p2
    end
    s5 = Student.find_or_create_by!(name: 'Fifth Student') do |s|
    s.project = p3
    end


    t1 = Teacher.find_or_create_by!(name: 'First Teacher') do |t|
    @@ -43,6 +49,9 @@
    t4 = Teacher.find_or_create_by!(name: 'Fourth Teacher') do |t|
    s.project = p2
    end
    t5 = Teacher.find_or_create_by!(name: 'Fifth Teacher') do |t|
    s.project = p3
    end


    st1 = Story.find_or_create_by!(name: 'First Story') do |st|
    @@ -57,6 +66,9 @@
    st4 = Story.find_or_create_by!(name: 'Fourth Story') do |st|
    s.project = p2
    end
    st5 = Story.find_or_create_by!(name: 'Fifth Story') do |st|
    s.project = p3
    end


    ta1 = Task.find_or_create_by!(name: 'First Task') do |ta|
    @@ -71,6 +83,9 @@
    ta4 = Task.find_or_create_by!(name: 'Fourth Task') do |ta|
    s.project = p2
    end
    ta5 = Task.find_or_create_by!(name: 'Fifth Task') do |ta|
    s.project = p3
    end



  21. phlegx revised this gist Feb 19, 2015. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions seeds.rb
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    p2 = Project.find_or_create_by!(name: 'Second Project')
    p3 = Project.find_or_create_by!(name: 'Third Project')


    u1 = User.find_or_create_by!(name: 'First User') do |u|
    s.project = p1
    end
    @@ -15,6 +16,7 @@
    s.project = p2
    end


    s1 = Student.find_or_create_by!(name: 'First Student') do |s|
    s.project = p1
    end
    @@ -28,6 +30,7 @@
    s.project = p2
    end


    t1 = Teacher.find_or_create_by!(name: 'First Teacher') do |t|
    s.project = p1
    end
    @@ -41,6 +44,7 @@
    s.project = p2
    end


    st1 = Story.find_or_create_by!(name: 'First Story') do |st|
    s.project = p1
    end
    @@ -54,6 +58,7 @@
    s.project = p2
    end


    ta1 = Task.find_or_create_by!(name: 'First Task') do |ta|
    s.project = p1
    end
  22. phlegx revised this gist Feb 19, 2015. 1 changed file with 17 additions and 4 deletions.
    21 changes: 17 additions & 4 deletions seeds.rb
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,19 @@
    p2 = Project.find_or_create_by!(name: 'Second Project')
    p3 = Project.find_or_create_by!(name: 'Third Project')

    u1 = User.find_or_create_by!(name: 'First User') do |u|
    s.project = p1
    end
    u2 = User.find_or_create_by!(name: 'Second User') do |u|
    s.project = p1
    end
    u3 = User.find_or_create_by!(name: 'Third User') do |u|
    s.project = p2
    end
    u4 = User.find_or_create_by!(name: 'Fourth User') do |u|
    s.project = p2
    end

    s1 = Student.find_or_create_by!(name: 'First Student') do |s|
    s.project = p1
    end
    @@ -41,16 +54,16 @@
    s.project = p2
    end

    ta1 = Task.find_or_create_by!(name: 'First Task') do |st|
    ta1 = Task.find_or_create_by!(name: 'First Task') do |ta|
    s.project = p1
    end
    ta2 = Task.find_or_create_by!(name: 'Second Task') do |st|
    ta2 = Task.find_or_create_by!(name: 'Second Task') do |ta|
    s.project = p1
    end
    ta3 = Task.find_or_create_by!(name: 'Third Task') do |st|
    ta3 = Task.find_or_create_by!(name: 'Third Task') do |ta|
    s.project = p2
    end
    ta4 = Task.find_or_create_by!(name: 'Fourth Task') do |st|
    ta4 = Task.find_or_create_by!(name: 'Fourth Task') do |ta|
    s.project = p2
    end

  23. phlegx revised this gist Feb 19, 2015. 1 changed file with 25 additions and 1 deletion.
    26 changes: 25 additions & 1 deletion seeds.rb
    Original file line number Diff line number Diff line change
    @@ -11,16 +11,22 @@
    s3 = Student.find_or_create_by!(name: 'Third Student') do |s|
    s.project = p2
    end
    s4 = Student.find_or_create_by!(name: 'Fourth Student') do |s|
    s.project = p2
    end

    t1 = Teacher.find_or_create_by!(name: 'First Teacher') do |t|
    s.project = p1
    end
    t2 = Teacher.find_or_create_by!(name: 'Second Teacher') do |t|
    s.project = p2
    s.project = p1
    end
    t3 = Teacher.find_or_create_by!(name: 'Third Teacher') do |t|
    s.project = p2
    end
    t4 = Teacher.find_or_create_by!(name: 'Fourth Teacher') do |t|
    s.project = p2
    end

    st1 = Story.find_or_create_by!(name: 'First Story') do |st|
    s.project = p1
    @@ -31,4 +37,22 @@
    st3 = Story.find_or_create_by!(name: 'Third Story') do |st|
    s.project = p2
    end
    st4 = Story.find_or_create_by!(name: 'Fourth Story') do |st|
    s.project = p2
    end

    ta1 = Task.find_or_create_by!(name: 'First Task') do |st|
    s.project = p1
    end
    ta2 = Task.find_or_create_by!(name: 'Second Task') do |st|
    s.project = p1
    end
    ta3 = Task.find_or_create_by!(name: 'Third Task') do |st|
    s.project = p2
    end
    ta4 = Task.find_or_create_by!(name: 'Fourth Task') do |st|
    s.project = p2
    end



  24. phlegx revised this gist Feb 19, 2015. 1 changed file with 34 additions and 0 deletions.
    34 changes: 34 additions & 0 deletions seeds.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    p1 = Project.find_or_create_by!(name: 'First Project')
    p2 = Project.find_or_create_by!(name: 'Second Project')
    p3 = Project.find_or_create_by!(name: 'Third Project')

    s1 = Student.find_or_create_by!(name: 'First Student') do |s|
    s.project = p1
    end
    s2 = Student.find_or_create_by!(name: 'Second Student') do |s|
    s.project = p1
    end
    s3 = Student.find_or_create_by!(name: 'Third Student') do |s|
    s.project = p2
    end

    t1 = Teacher.find_or_create_by!(name: 'First Teacher') do |t|
    s.project = p1
    end
    t2 = Teacher.find_or_create_by!(name: 'Second Teacher') do |t|
    s.project = p2
    end
    t3 = Teacher.find_or_create_by!(name: 'Third Teacher') do |t|
    s.project = p2
    end

    st1 = Story.find_or_create_by!(name: 'First Story') do |st|
    s.project = p1
    end
    st2 = Story.find_or_create_by!(name: 'Second Story') do |st|
    s.project = p1
    end
    st3 = Story.find_or_create_by!(name: 'Third Story') do |st|
    s.project = p2
    end

  25. phlegx revised this gist Feb 19, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion migrations.rb
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ def change
    create_table :tasks do |t|
    t.string :type
    t.references :project, index: true
    t.string: :name
    t.string :name
    t.timestamps
    end
    end
  26. phlegx revised this gist Feb 19, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions migrations.rb
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,7 @@ def change
    create_table :tasks do |t|
    t.string :type
    t.references :project, index: true
    t.string: :name
    t.timestamps
    end
    end
    @@ -22,6 +23,7 @@ def change
    create_table :users do |t|
    t.string :type
    t.references :project, index: true
    t.string :name
    t.timestamps
    end
    end
  27. phlegx revised this gist Feb 19, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions migrations.rb
    Original file line number Diff line number Diff line change
    @@ -7,9 +7,9 @@ def change
    end
    end

    class CreateLocations < ActiveRecord::Migration
    class CreateTasks < ActiveRecord::Migration
    def change
    create_table :locations do |t|
    create_table :tasks do |t|
    t.string :type
    t.references :project, index: true
    t.timestamps
  28. phlegx revised this gist Feb 19, 2015. 4 changed files with 37 additions and 9 deletions.
    4 changes: 2 additions & 2 deletions README
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    See question on stack overflow: http://stackoverflow.com/questions/28595636/rails-4-how-to-give-alias-names-to-includes-and-joins-in-active-record-que

    - Model Student and model Teacher are both STI models with super class model User
    - Model Place is a STI model with super class model Location
    - Model Story is a STI model with super class model Task
    - includes() and joins(), both fails

    Rails alias naming convention (includes() and joins())
    @@ -19,7 +19,7 @@ Rails alias naming convention (includes() and joins())
    - 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, :places))
    - 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.
    28 changes: 28 additions & 0 deletions migrations.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    class CreateProjects < ActiveRecord::Migration
    def change
    create_table :projects do |t|
    t.string :name
    t.timestamps
    end
    end
    end

    class CreateLocations < ActiveRecord::Migration
    def change
    create_table :locations do |t|
    t.string :type
    t.references :project, index: true
    t.timestamps
    end
    end
    end

    class CreateUsers < ActiveRecord::Migration
    def change
    create_table :users do |t|
    t.string :type
    t.references :project, index: true
    t.timestamps
    end
    end
    end
    6 changes: 3 additions & 3 deletions models.rb
    Original file line number Diff line number Diff line change
    @@ -3,14 +3,14 @@
    class Project < ActiveRecord::Base
    has_many :students
    has_many :teachers
    has_many :places
    has_many :stories
    end

    class Location < ActiveRecord::Base
    class Task < ActiveRecord::Base
    belongs_to :project
    end

    class Place < Location
    class Story < Task
    end

    class User < ActiveRecord::Base
    8 changes: 4 additions & 4 deletions quering.rb
    Original file line number Diff line number Diff line change
    @@ -21,20 +21,20 @@

    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, :places).order('locations.name ASC') # order on places
    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, :places).order('locations.name ASC') # order on places
    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, :places).order('places.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, :places).order('places.name ASC')
    Project.all.joins(:students, :stories).order('stories.name ASC')
  29. phlegx revised this gist Feb 19, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    See question on stack overflow: http://stackoverflow.com/questions/28595636/rails-4-how-to-give-alias-names-to-includes-and-joins-in-active-record-que

    - Model Student and model Teacher are both STI models with super class model User
    - Model Place is a STI model with super class model Location
    - includes() and joins(), both fails

    Rails alias naming convention (includes() and joins())
  30. phlegx revised this gist Feb 19, 2015. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion quering.rb
    Original file line number Diff line number Diff line change
    @@ -21,16 +21,20 @@

    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, :places).order('locations.name ASC') # order on places

    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, :places).order('locations.name ASC') # order on places

    # 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, :places).order('places.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, :teachers).order('students_projects.name ASC')
    Project.all.joins(:students, :places).order('places.name ASC')