diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-08-20 11:49:17 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-08-20 11:49:17 -0700 |
commit | 257366d118a9f45883ad4f1950c54ba07c248cbf (patch) | |
tree | 13b9fec2a5c0b1106ba7d715db6f8091cddf8816 | |
parent | 9ca44e36d364dc487aa1b8773db62e6c2557838f (diff) | |
parent | f74f5f7f00bd591fbca0cbc2b847acb0ec420256 (diff) | |
download | rails-257366d118a9f45883ad4f1950c54ba07c248cbf.tar.gz rails-257366d118a9f45883ad4f1950c54ba07c248cbf.tar.bz2 rails-257366d118a9f45883ad4f1950c54ba07c248cbf.zip |
Merge pull request #2602 from ernie/fix_predicate_builder_primary_key_assumption
Fix assumption of primary key name in PredicateBuilder subquery.
-rw-r--r-- | activerecord/lib/active_record/relation/predicate_builder.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 23 |
2 files changed, 24 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index 2814771002..7e8ddd1b5d 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -19,7 +19,7 @@ module ActiveRecord case value when ActiveRecord::Relation - value.select_values = [value.klass.arel_table['id']] if value.select_values.empty? + value = value.select(value.klass.arel_table[value.klass.primary_key]) if value.select_values.empty? attribute.in(value.arel.ast) when Array, ActiveRecord::Associations::CollectionProxy values = value.to_a.map { |x| diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 97abd67385..615551a279 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -540,6 +540,29 @@ class RelationTest < ActiveRecord::TestCase } end + def test_find_all_using_where_with_relation_and_alternate_primary_key + cool_first = minivans(:cool_first) + # switching the lines below would succeed in current rails + # assert_queries(2) { + assert_queries(1) { + relation = Minivan.where(:minivan_id => Minivan.where(:name => cool_first.name)) + assert_equal [cool_first], relation.all + } + end + + def test_find_all_using_where_with_relation_does_not_alter_select_values + david = authors(:david) + + subquery = Author.where(:id => david.id) + + assert_queries(1) { + relation = Author.where(:id => subquery) + assert_equal [david], relation.all + } + + assert_equal 0, subquery.select_values.size + end + def test_find_all_using_where_with_relation_with_joins david = authors(:david) assert_queries(1) { |