mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #399 from ernie/join_conditions_on_join
Add association join conditions to JOIN clause, not WHERE
This commit is contained in:
commit
030e1d06ca
2 changed files with 14 additions and 3 deletions
|
@ -91,12 +91,12 @@ module ActiveRecord
|
|||
|
||||
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
|
||||
|
|
|
@ -34,6 +34,17 @@ class InnerJoinAssociationTest < ActiveRecord::TestCase
|
|||
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"
|
||||
|
|
Loading…
Reference in a new issue