Skip to content

Commit

Permalink
Merge pull request #2605 from ernie/3-1-stable-2602-backport
Browse files Browse the repository at this point in the history
3-1-stable 2602 backport
  • Loading branch information
tenderlove committed Aug 20, 2011
2 parents f8f1e18 + bf4714f commit 1d354fa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Expand Up @@ -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|
Expand Down
23 changes: 23 additions & 0 deletions activerecord/test/cases/relations_test.rb
Expand Up @@ -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) {
Expand Down

0 comments on commit 1d354fa

Please sign in to comment.