Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. Hello
  2.  
  3. I would like to preload associations but if no associations exist I would like to return the inital AR relation.
  4.  
  5. To understand my issue here is a reproducible script :
  6.  
  7. ```ruby
  8. gem 'rails'
  9. require 'active_record'
  10.  
  11. puts "Active Record #{ActiveRecord::VERSION::STRING}"
  12.  
  13. ActiveRecord::Base.establish_connection(
  14. adapter: 'sqlite3',
  15. database: ':memory:'
  16. )
  17.  
  18. ActiveRecord::Schema.define do
  19. create_table :organisations, force: true do |t|
  20. t.string 'name', limit: 255, null: false
  21. t.datetime 'created_at'
  22. t.datetime 'updated_at'
  23. end
  24.  
  25. create_table :groups, force: true do |t|
  26. t.string 'name', limit: 255, null: false
  27. t.integer 'shop_id', null: false
  28. t.datetime 'created_at'
  29. t.datetime 'updated_at'
  30. end
  31.  
  32. create_table :groups_organisations, force: true do |t|
  33. t.integer 'organisation_id', null: false
  34. t.integer 'group_id', null: false
  35. t.datetime 'created_at'
  36. t.datetime 'updated_at'
  37. end
  38. end
  39.  
  40. class Organisation < ActiveRecord::Base
  41. has_many :groups_organisations
  42. has_many :groups, through: :groups_organisations
  43. end
  44.  
  45. class Group < ActiveRecord::Base
  46. has_many :groups_organisations
  47. has_many :organisation, through: :groups_organisations
  48. end
  49.  
  50. class GroupsOrganisation < ActiveRecord::Base
  51. belongs_to :group
  52. belongs_to :organisation
  53. end
  54. organisation_without_groups = Organisation.create!(name: 'my organisation without groups')
  55. group_1 = Group.create!(name: 'group_1', shop_id: 1)
  56. group_2 = Group.create!(name: 'group_2', shop_id: 2)
  57. organisation_with_groups = Organisation.create!(name: 'my organisation with groups', groups: [group_1, group_2])
  58. ```
  59. When I don't have records associated
  60.  
  61. ```ruby
  62. p Organisation.where(name: 'my organisation without groups').includes(:groups).where(groups: { shop_id: 1 })
  63. # <ActiveRecord::Relation []>
  64. ```
  65. When I have records to include
  66. ```ruby
  67. p Organisation.where(name: 'my organisation with groups').includes(:groups).where(groups: { shop_id: 1 })
  68. # <ActiveRecord::Relation [#<Organisation id: 2, name: "my organisation with groups", created_at: "2017-07-21 09:45:07", updated_at: "2017-07-21 09:45:07">]>
  69. ```
  70.  
  71. Where clause is performed on all the query not on the LEFT OUTER JOIN
  72. ```sql
  73.  
  74. SELECT "organisations"."id" AS t0_r0,
  75. "organisations"."name" AS t0_r1,
  76. "organisations"."created_at" AS t0_r2,
  77. "organisations"."updated_at" AS t0_r3,
  78. "groups"."id" AS t1_r0,
  79. "groups"."name" AS t1_r1,
  80. "groups"."shop_id" AS t1_r2,
  81. "groups"."created_at" AS t1_r3,
  82. "groups"."updated_at" AS t1_r4
  83. FROM "organisations"
  84. LEFT OUTER JOIN "groups_organisations"
  85. ON "groups_organisations"."organisation_id" = "organisations"."id"
  86. LEFT OUTER JOIN "groups"
  87. ON "groups"."id" = "groups_organisations"."group_id"
  88. WHERE "organisations"."name" = 'my organisation without groups'
  89. AND "groups"."shop_id" = 1
  90. ```
  91. What I would like is :
  92.  
  93. ```sql
  94. LEFT OUTER JOIN "groups"
  95. ON "groups"."id" = "groups_organisations"."group_id"
  96. AND groups.store_id = 20441
  97. WHERE "organisations"."name" = 'my organisation without groups'
  98. ```
  99. Is it possible to preload using LEFT OUTER JOIN with conditionals?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement