Skip to content

Commit

Permalink
Support for multiple selects added
Browse files Browse the repository at this point in the history
[#4841 state:committed]
  • Loading branch information
Neeraj Singh and Santiago Pastorino authored and spastorino committed Jun 25, 2010
1 parent 3d8ccb9 commit 0ebb5bf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
11 changes: 7 additions & 4 deletions activerecord/lib/active_record/relation/query_methods.rb
Expand Up @@ -213,13 +213,16 @@ def build_joins(relation, joins)
def build_select(arel, selects)
if selects.present?
@implicit_readonly = false
selects.each do |s|
arel = arel.project(s) if s.present?
# TODO: fix this ugly hack, we should refactor the callers to get an ARel compatible array.
# Before this change we were passing to ARel the last element only, and ARel is capable of handling an array
if selects.all? { |s| s.is_a?(String) || !s.is_a?(Arel::Expression) } && !(selects.last =~ /^COUNT\(/)
arel.project(*selects)
else
arel.project(selects.last)
end
else
arel = arel.project(@klass.quoted_table_name + '.*')
arel.project(@klass.quoted_table_name + '.*')
end
arel
end

def apply_modules(modules)
Expand Down
8 changes: 5 additions & 3 deletions activerecord/test/cases/method_scoping_test.rb
Expand Up @@ -77,11 +77,13 @@ def test_scoped_find_select
end
end

def test_options_select_replaces_scope_select
Developer.send(:with_scope, :find => { :select => "id, name" }) do
def test_scope_select_concatenates
Developer.send(:with_scope, :find => { :select => "name" }) do
developer = Developer.find(:first, :select => 'id, salary', :conditions => "name = 'David'")
assert_equal 80000, developer.salary
assert !developer.has_attribute?(:name)
assert developer.has_attribute?(:id)
assert developer.has_attribute?(:name)
assert developer.has_attribute?(:salary)
end
end

Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/relations_test.rb
Expand Up @@ -491,6 +491,12 @@ def test_count_explicit_columns
assert_equal 0, posts.count('comments_count')
end

def test_multiple_selects
post = Post.scoped.select('comments_count').select('title').order("id ASC").first
assert_equal "Welcome to the weblog", post.title
assert_equal 2, post.comments_count
end

def test_size
posts = Post.scoped

Expand Down

0 comments on commit 0ebb5bf

Please sign in to comment.