From a075503c75c6f7edf7ff843ad5559b5d760dc9c5 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Thu, 29 May 2008 12:59:29 +0100 Subject: [PATCH] Changed precedence of :select option in construct_finder_sql so that :select passed in options overrides the :select option from the scope --- activerecord/lib/active_record/base.rb | 2 +- activerecord/test/cases/method_scoping_test.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index c393128..d2d9bda 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1457,7 +1457,7 @@ module ActiveRecord #:nodoc: def construct_finder_sql(options) scope = scope(:find) - sql = "SELECT #{(scope && scope[:select]) || options[:select] || (options[:joins] && quoted_table_name + '.*') || '*'} " + sql = "SELECT #{options[:select] || (scope && scope[:select]) || (options[:joins] && quoted_table_name + '.*') || '*'} " sql << "FROM #{(scope && scope[:from]) || options[:from] || quoted_table_name} " add_joins!(sql, options, scope) diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index 4b5bd6c..ded273e 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -50,6 +50,22 @@ class MethodScopingTest < ActiveRecord::TestCase end end + def test_scoped_find_select + Developer.with_scope(:find => { :select => "id, name" }) do + assert_match /^SELECT id, name FROM/, Developer.send(:construct_finder_sql, {}) + assert Developer.find(:first, :conditions => "name = 'David'").has_attribute?(:name) + assert !Developer.find(:first, :conditions => "name = 'David'").has_attribute?(:salary) + end + end + + def test_options_select_replaces_scope_select + Developer.with_scope(:find => { :select => "id, name" }) do + assert_match /^SELECT id, salary FROM/, Developer.send(:construct_finder_sql, { :select => "id, salary" }) + assert Developer.find(:first, :select => 'id, salary', :conditions => "name = 'David'").has_attribute?(:salary) + assert !Developer.find(:first, :select => 'id, salary', :conditions => "name = 'David'").has_attribute?(:name) + end + end + def test_scoped_count Developer.with_scope(:find => { :conditions => "name = 'David'" }) do assert_equal 1, Developer.count -- 1.5.4.5