aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorNeeraj Singh and Santiago Pastorino <neeraj+santiago@wyeworks.com>2010-06-24 22:52:15 -0300
committerSantiago Pastorino <santiago@wyeworks.com>2010-06-25 15:57:04 -0300
commit0ebb5bf6590b8ac62c53538ade7095676baec3d4 (patch)
treed5cb0d2659f27faa00cdf9dc54c7247e042b954d /activerecord
parent3d8ccb924084ecd341b2d9644e6e0b66903d8432 (diff)
downloadrails-0ebb5bf6590b8ac62c53538ade7095676baec3d4.tar.gz
rails-0ebb5bf6590b8ac62c53538ade7095676baec3d4.tar.bz2
rails-0ebb5bf6590b8ac62c53538ade7095676baec3d4.zip
Support for multiple selects added
[#4841 state:committed]
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb11
-rw-r--r--activerecord/test/cases/method_scoping_test.rb8
-rw-r--r--activerecord/test/cases/relations_test.rb6
3 files changed, 18 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 1e570a569b..4dbb30c777 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -213,13 +213,16 @@ module ActiveRecord
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)
diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb
index e93f22bede..6cd42ff936 100644
--- a/activerecord/test/cases/method_scoping_test.rb
+++ b/activerecord/test/cases/method_scoping_test.rb
@@ -77,11 +77,13 @@ class MethodScopingTest < ActiveRecord::TestCase
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
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index abf43cea98..5b1c6b8f22 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -491,6 +491,12 @@ class RelationTest < ActiveRecord::TestCase
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