File tree Expand file tree Collapse file tree 9 files changed +50
-4
lines changed
app/services/forest_liana Expand file tree Collapse file tree 9 files changed +50
-4
lines changed Original file line number Diff line number Diff line change @@ -32,15 +32,23 @@ def compute_includes
3232
3333 def optimize_record_loading ( resource , records )
3434 instance_dependent_associations = instance_dependent_associations ( resource )
35- eager_loads = @includes - instance_dependent_associations
3635
37- result = records . eager_load ( eager_loads )
36+ preload_loads = @includes . select do |name |
37+ targetModelConnection = resource . reflect_on_association ( name ) . inverse_of &.active_record &.connection
38+ targetModelDatabase = targetModelConnection . current_database if targetModelConnection . respond_to? :current_database
39+ resourceConnection = resource . connection
40+ resourceDatabase = resourceConnection if resourceConnection . respond_to? :current_database
41+
42+ targetModelDatabase != resourceDatabase
43+ end + instance_dependent_associations
44+
45+ result = records . eager_load ( @includes - preload_loads )
3846
3947 # Rails 7 can mix `eager_load` and `preload` in the same scope
4048 # Rails 6 cannot mix `eager_load` and `preload` in the same scope
4149 # Rails 6 and 7 cannot mix `eager_load` and `includes` in the same scope
4250 if Rails ::VERSION ::MAJOR >= 7
43- result = result . preload ( instance_dependent_associations )
51+ result = result . preload ( preload_loads )
4452 end
4553
4654 result
Original file line number Diff line number Diff line change 1+ class Car < GarageRecord
2+ belongs_to :driver
3+ end
Original file line number Diff line number Diff line change 1+ class Driver < UserRecord
2+ has_one :car
3+ end
Original file line number Diff line number Diff line change 1+ class GarageRecord < ApplicationRecord
2+ self . abstract_class = true
3+ connects_to database : { writing : :garage , reading : :garage }
4+ end
Original file line number Diff line number Diff line change 1+ class UserRecord < ApplicationRecord
2+ self . abstract_class = true
3+ connects_to database : { writing : :user , reading : :user }
4+ end
Original file line number Diff line number Diff line change @@ -19,6 +19,13 @@ development:
1919test :
2020 << : *default
2121 database : db/test.sqlite3
22+
23+ garage :
24+ << : *default
25+ database : db/test2.sqlite3
26+ user :
27+ << : *default
28+ database : db/test3.sqlite3
2229
2330production :
2431 << : *default
Original file line number Diff line number Diff line change 1+ class CreateCars < ActiveRecord ::Migration [ 6.0 ]
2+ def change
3+ Car . connection . create_table :cars do |t |
4+ t . string :model
5+ t . references :driver , index : true
6+ end
7+ end
8+ end
Original file line number Diff line number Diff line change 1+ class CreateDrivers < ActiveRecord ::Migration [ 6.0 ]
2+ def change
3+ Driver . connection . create_table :drivers do |t |
4+ t . string :firstname
5+ end
6+ end
7+ end
Original file line number Diff line number Diff line change @@ -30,7 +30,9 @@ module ForestLiana
3030 Reference ,
3131 Town ,
3232 Tree ,
33- User
33+ User ,
34+ Driver ,
35+ Car ,
3436 ]
3537 end
3638
You can’t perform that action at this time.
0 commit comments