diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-06-27 20:13:17 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-06-27 20:32:17 +0900 |
commit | 442c15f1419ae60e4345866e5af59376a679e9d1 (patch) | |
tree | 993145931beba3aff0d463151af068e7cd3c183f | |
parent | 23bcc6578e28355cc7258c744bcb26c0c80524ef (diff) | |
download | rails-442c15f1419ae60e4345866e5af59376a679e9d1.tar.gz rails-442c15f1419ae60e4345866e5af59376a679e9d1.tar.bz2 rails-442c15f1419ae60e4345866e5af59376a679e9d1.zip |
Move building constraint to `join_scope` in `Reflection`
-rw-r--r-- | activerecord/lib/active_record/associations/join_dependency/join_association.rb | 42 | ||||
-rw-r--r-- | activerecord/lib/active_record/reflection.rb | 13 |
2 files changed, 14 insertions, 41 deletions
diff --git a/activerecord/lib/active_record/associations/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/join_dependency/join_association.rb index 70520b8e85..005410d598 100644 --- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb @@ -34,18 +34,10 @@ module ActiveRecord table = tables.shift klass = reflection.klass - join_keys = reflection.join_keys - key = join_keys.key - foreign_key = join_keys.foreign_key + join_scope = reflection.join_scope(table, foreign_table, foreign_klass) - constraint = build_constraint(klass, table, key, foreign_table, foreign_key) - - rel = reflection.join_scope(table, foreign_klass) - - if rel.arel.constraints.any? - binds += rel.bound_attributes - constraint = constraint.and rel.arel.constraints - end + binds.concat join_scope.bound_attributes + constraint = join_scope.arel.constraints joins << table.create_join(table, table.create_on(constraint), join_type) @@ -56,34 +48,6 @@ module ActiveRecord JoinInformation.new joins, binds end - # Builds equality condition. - # - # Example: - # - # class Physician < ActiveRecord::Base - # has_many :appointments - # end - # - # If I execute `Physician.joins(:appointments).to_a` then - # klass # => Physician - # table # => #<Arel::Table @name="appointments" ...> - # key # => physician_id - # foreign_table # => #<Arel::Table @name="physicians" ...> - # foreign_key # => id - # - def build_constraint(klass, table, key, foreign_table, foreign_key) - constraint = table[key].eq(foreign_table[foreign_key]) - - if klass.finder_needs_type_condition? - constraint = table.create_and([ - constraint, - klass.send(:type_condition, table) - ]) - end - - constraint - end - def table tables.first end diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 4e3359bb4e..3d3ec862a3 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -171,7 +171,7 @@ module ActiveRecord JoinKeys = Struct.new(:key, :foreign_key) # :nodoc: def join_keys - get_join_keys klass + @join_keys ||= get_join_keys(klass) end # Returns a list of scopes that should be applied for this Reflection @@ -185,11 +185,20 @@ module ActiveRecord end deprecate :scope_chain - def join_scope(table, foreign_klass) + def join_scope(table, foreign_table, foreign_klass) predicate_builder = predicate_builder(table) scope_chain_items = join_scopes(table, predicate_builder) klass_scope = klass_join_scope(table, predicate_builder) + key = join_keys.key + foreign_key = join_keys.foreign_key + + klass_scope.where!(table[key].eq(foreign_table[foreign_key])) + + if klass.finder_needs_type_condition? + klass_scope.where!(klass.send(:type_condition, table)) + end + if type klass_scope.where!(type => foreign_klass.base_class.name) end |