Skip to content

Commit

Permalink
Add join conditions to JOIN clause, not WHERE
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernie Miller committed May 5, 2011
1 parent ee898e5 commit 8d270a2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Expand Up @@ -91,12 +91,12 @@ def join_to(relation)


constraint = build_constraint(reflection, table, key, foreign_table, foreign_key) constraint = build_constraint(reflection, table, key, foreign_table, foreign_key)


relation.from(join(table, constraint))

unless conditions[i].empty? unless conditions[i].empty?
relation.where(sanitize(conditions[i], table)) constraint = constraint.and(sanitize(conditions[i], table))
end end


relation.from(join(table, constraint))

# The current table in this iteration becomes the foreign table in the next # The current table in this iteration becomes the foreign table in the next
foreign_table = table foreign_table = table
end end
Expand Down
Expand Up @@ -34,6 +34,12 @@ def test_construct_finder_sql_ignores_empty_joins_array
assert_no_match(/JOIN/i, sql) assert_no_match(/JOIN/i, sql)
end 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 def test_find_with_implicit_inner_joins_honors_readonly_without_select
authors = Author.joins(:posts).to_a authors = Author.joins(:posts).to_a
assert !authors.empty?, "expected authors to be non-empty" assert !authors.empty?, "expected authors to be non-empty"
Expand Down

0 comments on commit 8d270a2

Please sign in to comment.