Skip to content
Browse files

Merge pull request #399 from ernie/join_conditions_on_join

Add association join conditions to JOIN clause, not WHERE
  • Loading branch information...
2 parents f098c80 + 9b7cd75 commit 030e1d06ca36e1ed235f8e34ac9bfb6906b12dce @jonleighton jonleighton committed May 5, 2011
View
6 activerecord/lib/active_record/associations/join_dependency/join_association.rb
@@ -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
View
11 activerecord/test/cases/associations/inner_join_association_test.rb
@@ -34,6 +34,17 @@ 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_join_conditions_allow_nil_associations
+ authors = Author.includes(:essays).where(:essays => {:id => nil})
+ assert_equal 2, authors.count
+ 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"

0 comments on commit 030e1d0

Please sign in to comment.
Something went wrong with that request. Please try again.