diff options
author | Ernie Miller <ernie@metautonomo.us> | 2011-05-05 12:35:29 -0400 |
---|---|---|
committer | Ernie Miller <ernie@metautonomo.us> | 2011-05-05 12:35:29 -0400 |
commit | 8d270a2abbc1601777f4918bd1968c18f7864ae5 (patch) | |
tree | afe26d262bd63b3ecf62fad368143d1c5c3c9ec0 | |
parent | ee898e5248d3876372c8303f500afe2baf6f298c (diff) | |
download | rails-8d270a2abbc1601777f4918bd1968c18f7864ae5.tar.gz rails-8d270a2abbc1601777f4918bd1968c18f7864ae5.tar.bz2 rails-8d270a2abbc1601777f4918bd1968c18f7864ae5.zip |
Add join conditions to JOIN clause, not WHERE
-rw-r--r-- | activerecord/lib/active_record/associations/join_dependency/join_association.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/associations/inner_join_association_test.rb | 6 |
2 files changed, 9 insertions, 3 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 0a666598ed..c32753782f 100644 --- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb @@ -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 diff --git a/activerecord/test/cases/associations/inner_join_association_test.rb b/activerecord/test/cases/associations/inner_join_association_test.rb index e2228228a3..cc19c874f0 100644 --- a/activerecord/test/cases/associations/inner_join_association_test.rb +++ b/activerecord/test/cases/associations/inner_join_association_test.rb @@ -34,6 +34,12 @@ 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_find_with_implicit_inner_joins_honors_readonly_without_select authors = Author.joins(:posts).to_a assert !authors.empty?, "expected authors to be non-empty" |