Browse files

Fix assumption of primary key name in PredicateBuilder subquery.

  • Loading branch information...
1 parent 8bd76aa commit e2ae015952c5bdcca044504b5be1c13a518eeaac @ernie ernie committed Aug 20, 2011
View
2 activerecord/lib/active_record/relation/predicate_builder.rb
@@ -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.select_values = [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|
View
10 activerecord/test/cases/relations_test.rb
@@ -540,6 +540,16 @@ 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_with_joins
david = authors(:david)
assert_queries(1) {

0 comments on commit e2ae015

Please sign in to comment.