Last active
May 8, 2025 17:02
-
-
Save phlegx/add77d24ebc57f211e8b to your computer and use it in GitHub Desktop.
Revisions
-
phlegx revised this gist
Feb 21, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(p.join(s).on(p[:id].eq(s[:project_id]).and(s[:type].in('Student'))).join_sources) -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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]).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) -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(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 -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
phlegx revised this gist
Feb 19, 2015 . 2 changed files with 10 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 6 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 Some tests: - Project.joins('INNER JOIN "users" AS "students" ON "students"."project_id" = "projects"."id" AND "students"."type" IN ("Student")').order('students.name DESC') -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 20 additions and 20 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,19 +4,19 @@ u1 = User.find_or_create_by!(name: 'First User') do |u| u.project = p1 end u2 = User.find_or_create_by!(name: 'Second User') do |u| u.project = p1 end u3 = User.find_or_create_by!(name: 'Third User') do |u| u.project = p2 end u4 = User.find_or_create_by!(name: 'Fourth User') do |u| u.project = p2 end u5 = User.find_or_create_by!(name: 'Fifth User') do |u| u.project = p3 end @@ -38,53 +38,53 @@ t1 = Teacher.find_or_create_by!(name: 'First Teacher') do |t| t.project = p1 end t2 = Teacher.find_or_create_by!(name: 'Second Teacher') do |t| t.project = p1 end t3 = Teacher.find_or_create_by!(name: 'Third Teacher') do |t| t.project = p2 end t4 = Teacher.find_or_create_by!(name: 'Fourth Teacher') do |t| t.project = p2 end t5 = Teacher.find_or_create_by!(name: 'Fifth Teacher') do |t| t.project = p3 end st1 = Story.find_or_create_by!(name: 'First Story') do |st| st.project = p1 end st2 = Story.find_or_create_by!(name: 'Second Story') do |st| st.project = p1 end st3 = Story.find_or_create_by!(name: 'Third Story') do |st| st.project = p2 end st4 = Story.find_or_create_by!(name: 'Fourth Story') do |st| st.project = p2 end st5 = Story.find_or_create_by!(name: 'Fifth Story') do |st| st.project = p3 end ta1 = Task.find_or_create_by!(name: 'First Task') do |ta| ta.project = p1 end ta2 = Task.find_or_create_by!(name: 'Second Task') do |ta| ta.project = p1 end ta3 = Task.find_or_create_by!(name: 'Third Task') do |ta| ta.project = p2 end ta4 = Task.find_or_create_by!(name: 'Fourth Task') do |ta| ta.project = p2 end ta5 = Task.find_or_create_by!(name: 'Fifth Task') do |ta| ta.project = p3 end -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 - 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 -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 - http://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-the-joined-tables -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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/blob/master/guides/bug_report_templates/active_record_gem.rb -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. Some links: - https://github.com/rails/rails/issues/606 -
phlegx revised this gist
Feb 19, 2015 . 2 changed files with 7 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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| This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
phlegx renamed this gist
Feb 19, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 15 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 17 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 |ta| s.project = p1 end 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 |ta| s.project = p2 end ta4 = Task.find_or_create_by!(name: 'Fourth Task') do |ta| s.project = p2 end -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 25 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 = 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 -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 34 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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.timestamps end end -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,9 +7,9 @@ def change end end class CreateTasks < ActiveRecord::Migration def change create_table :tasks do |t| t.string :type t.references :project, index: true t.timestamps -
phlegx revised this gist
Feb 19, 2015 . 4 changed files with 37 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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, :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. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 :stories end class Task < ActiveRecord::Base belongs_to :project end class Story < Task end class User < ActiveRecord::Base This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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, :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') -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()) -
phlegx revised this gist
Feb 19, 2015 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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, :places).order('places.name ASC')
NewerOlder