Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #2602 from ernie/fix_predicate_builder_primary_key…

…_assumption

Fix assumption of primary key name in PredicateBuilder subquery.
  • Loading branch information...
commit bf4714fe8214a4d92693541b6c9ba613bdc64ec3 1 parent d29eec6
Aaron Patterson tenderlove authored committed
2  activerecord/lib/active_record/relation/predicate_builder.rb
View
@@ -19,7 +19,7 @@ def self.build_from_hash(engine, attributes, default_table)
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|
23 activerecord/test/cases/relations_test.rb
View
@@ -531,6 +531,29 @@ def test_find_all_using_where_with_relation
}
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) {
Please sign in to comment.
Something went wrong with that request. Please try again.