aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/lib/active_record/associations/join_dependency/join_association.rb12
-rw-r--r--activerecord/lib/active_record/reflection.rb11
-rw-r--r--activerecord/lib/active_record/scoping/named.rb3
3 files changed, 10 insertions, 16 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 95f16c622e..70520b8e85 100644
--- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb
+++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb
@@ -40,21 +40,13 @@ module ActiveRecord
constraint = build_constraint(klass, table, key, foreign_table, foreign_key)
- rel = reflection.join_scope(table)
+ rel = reflection.join_scope(table, foreign_klass)
- if rel && !rel.arel.constraints.empty?
+ if rel.arel.constraints.any?
binds += rel.bound_attributes
constraint = constraint.and rel.arel.constraints
end
- if reflection.type
- value = foreign_klass.base_class.name
- column = klass.columns_hash[reflection.type.to_s]
-
- binds << Relation::QueryAttribute.new(column.name, value, klass.type_for_attribute(column.name))
- constraint = constraint.and klass.arel_attribute(reflection.type, table).eq(Arel::Nodes::BindParam.new)
- end
-
joins << table.create_join(table, table.create_on(constraint), join_type)
# The current table in this iteration becomes the foreign table in the next
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index c6aa2ed720..4e3359bb4e 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -185,12 +185,16 @@ module ActiveRecord
end
deprecate :scope_chain
- def join_scope(table)
+ def join_scope(table, foreign_klass)
predicate_builder = predicate_builder(table)
scope_chain_items = join_scopes(table, predicate_builder)
klass_scope = klass_join_scope(table, predicate_builder)
- scope_chain_items.inject(klass_scope || scope_chain_items.shift, &:merge!)
+ if type
+ klass_scope.where!(type => foreign_klass.base_class.name)
+ end
+
+ scope_chain_items.inject(klass_scope, &:merge!)
end
def join_scopes(table, predicate_builder) # :nodoc:
@@ -207,8 +211,7 @@ module ActiveRecord
scope.joins_values = scope.left_outer_joins_values = [].freeze
}
else
- relation = build_scope(table, predicate_builder)
- klass.send(:build_default_scope, relation)
+ klass.default_scoped(build_scope(table, predicate_builder))
end
end
diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb
index a61fdd6454..388f471bf5 100644
--- a/activerecord/lib/active_record/scoping/named.rb
+++ b/activerecord/lib/active_record/scoping/named.rb
@@ -29,8 +29,7 @@ module ActiveRecord
end
end
- def default_scoped # :nodoc:
- scope = relation
+ def default_scoped(scope = relation) # :nodoc:
build_default_scope(scope) || scope
end