Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add join conditions to JOIN clause, not WHERE

  • Loading branch information...
commit 8d270a2abbc1601777f4918bd1968c18f7864ae5 1 parent ee898e5
Ernie Miller ernie authored
6 activerecord/lib/active_record/associations/join_dependency/join_association.rb
View
@@ -91,12 +91,12 @@ def join_to(relation)
constraint = build_constraint(reflection, table, key, foreign_table, foreign_key)
- relation.from(join(table, constraint))
-
unless conditions[i].empty?
- relation.where(sanitize(conditions[i], table))
+ constraint = constraint.and(sanitize(conditions[i], table))
end
+ relation.from(join(table, constraint))
+
# The current table in this iteration becomes the foreign table in the next
foreign_table = table
end
6 activerecord/test/cases/associations/inner_join_association_test.rb
View
@@ -34,6 +34,12 @@ def test_construct_finder_sql_ignores_empty_joins_array
assert_no_match(/JOIN/i, sql)
end
+ def test_join_conditions_added_to_join_clause
+ sql = Author.joins(:essays).to_sql
+ assert_match(/writer_type.*?=.*?Author/i, sql)
+ assert_no_match(/WHERE/i, sql)
+ end
+
def test_find_with_implicit_inner_joins_honors_readonly_without_select
authors = Author.joins(:posts).to_a
assert !authors.empty?, "expected authors to be non-empty"
Please sign in to comment.
Something went wrong with that request. Please try again.